Skip to content

Commit df73f30

Browse files
CCM-12225: Fix tests
1 parent 14cf512 commit df73f30

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

lambdas/backend-api/src/__tests__/templates/infra/template-repository.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,7 @@ describe('templateRepository', () => {
21082108
'#templateType': 'templateType',
21092109
'#updatedAt': 'updatedAt',
21102110
'#proofingEnabled': 'proofingEnabled',
2111+
'#supplierReferences': 'supplierReferences',
21112112
},
21122113
ExpressionAttributeValues: {
21132114
':condition_1_templateStatus': 'PENDING_PROOF_REQUEST',
@@ -2116,13 +2117,14 @@ describe('templateRepository', () => {
21162117
':condition_5_proofingEnabled': true,
21172118
':templateStatus': 'WAITING_FOR_PROOF',
21182119
':updatedAt': '2024-12-27T00:00:00.000Z',
2120+
':supplierReferences': {},
21192121
},
21202122
Key: { id: 'template-id', owner: ownerWithClientPrefix },
21212123
ReturnValues: 'ALL_NEW',
21222124
ReturnValuesOnConditionCheckFailure: 'ALL_OLD',
21232125
TableName: 'templates',
21242126
UpdateExpression:
2125-
'SET #templateStatus = :templateStatus, #updatedAt = :updatedAt',
2127+
'SET #templateStatus = :templateStatus, #supplierReferences = if_not_exists(#supplierReferences, :supplierReferences), #updatedAt = :updatedAt',
21262128
});
21272129
});
21282130

lambdas/backend-api/src/templates/infra/template-repository.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,11 @@ export class TemplateRepository {
652652
)
653653
.setStatus('WAITING_FOR_PROOF')
654654
.expectedStatus('PENDING_PROOF_REQUEST')
655+
656+
// dynamodb does not support conditional initialising of maps, so we have to
657+
// initialise an empty map here, then we set supplier-specific values in the
658+
// per-supplier sftp send lambda
659+
.initialiseSupplierReferences()
655660
.expectedTemplateType('LETTER')
656661
.expectedClientId(user.clientId)
657662
.expectTemplateExists()

utils/entity-update-command-builder/src/__tests__/common/update-command-builder.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ describe('UpdateExpressionBuilder', () => {
105105
});
106106
});
107107

108+
describe('setValueIfNotExists', () => {
109+
test("sets value if it doesn't exist", () => {
110+
const builder = new UpdateCommandBuilder<Record<string, string>>(
111+
mockTableName,
112+
mockEntityKeys
113+
);
114+
115+
const res = builder.setValueIfNotExists('newKey', 'newValue').finalise();
116+
117+
expect(res).toEqual({
118+
TableName: mockTableName,
119+
Key: mockEntityKeys,
120+
ExpressionAttributeValues: {
121+
':newKey': 'newValue',
122+
},
123+
ExpressionAttributeNames: {
124+
'#newKey': 'newKey',
125+
},
126+
UpdateExpression: 'SET #newKey = if_not_exists(#newKey, :newKey)',
127+
});
128+
});
129+
});
130+
108131
describe('conditions', () => {
109132
const operandTestCases: [op: ConditionOperator, negated: boolean][] = [
110133
['<', false],

utils/entity-update-command-builder/src/__tests__/template-update-builder.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@ describe('TemplateUpdateBuilder', () => {
200200
});
201201
});
202202

203+
describe('initialiseSupplierReferences', () => {
204+
test('initialises supplier references', () => {
205+
const builder = new TemplateUpdateBuilder(
206+
mockTableName,
207+
mockOwner,
208+
mockId
209+
);
210+
211+
const res = builder.initialiseSupplierReferences().build();
212+
213+
expect(res).toEqual({
214+
ExpressionAttributeNames: {
215+
'#supplierReferences': 'supplierReferences',
216+
'#updatedAt': 'updatedAt',
217+
},
218+
ExpressionAttributeValues: {
219+
':supplierReferences': {},
220+
':updatedAt': '2025-01-01T09:00:00.000Z',
221+
},
222+
Key: {
223+
id: 'Hello2',
224+
owner: 'Hello1',
225+
},
226+
TableName: 'TABLE_NAME',
227+
UpdateExpression:
228+
'SET #supplierReferences = if_not_exists(#supplierReferences, :supplierReferences), #updatedAt = :updatedAt',
229+
});
230+
});
231+
});
232+
203233
describe('setLockTime', () => {
204234
test('sets lock time if no lock exists', () => {
205235
const builder = new TemplateUpdateBuilder(

utils/entity-update-command-builder/src/common/update-command-builder.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ export class UpdateCommandBuilder<Entity> {
4545
return this;
4646
}
4747

48+
setValueIfNotExists<T extends Prop<Entity>, K extends PropType<Entity, T>>(
49+
attributeName: T,
50+
value: K
51+
) {
52+
this._expressionAttributeNames[`#${attributeName}`] = attributeName;
53+
this._expressionAttributeValues[`:${attributeName}`] = value;
54+
this._updateExpressionSet.SET[attributeName] =
55+
`#${attributeName} = if_not_exists(#${attributeName}, :${attributeName})`;
56+
return this;
57+
}
58+
4859
addToValue<T extends Prop<Entity>, K extends PropType<Entity, T>>(
4960
attributeName: T,
5061
value: K

utils/entity-update-command-builder/src/template-update-builder.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export class TemplateUpdateBuilder extends EntityUpdateBuilder<DatabaseTemplate>
4949
return this;
5050
}
5151

52+
initialiseSupplierReferences() {
53+
this.updateBuilder.setValueIfNotExists('supplierReferences', {});
54+
return this;
55+
}
56+
5257
setSupplierReference(supplier: string, supplierReference: string) {
5358
this.updateBuilder.setValueInMap(
5459
'supplierReferences',

0 commit comments

Comments
 (0)