Skip to content

Commit 6b1188e

Browse files
CCM-12225: Add supplierReferences to template
1 parent 7c4b829 commit 6b1188e

File tree

24 files changed

+292
-132
lines changed

24 files changed

+292
-132
lines changed

infrastructure/terraform/modules/backend-api/spec.tmpl.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@
216216
},
217217
"proofingEnabled": {
218218
"type": "boolean"
219+
},
220+
"supplierReferences": {
221+
"additionalProperties": {
222+
"type": "string"
223+
},
224+
"type": "object"
219225
}
220226
},
221227
"required": [

lambdas/backend-client/src/types/generated/types.gen.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export type LetterProperties = UploadLetterProperties & {
8080
files: LetterFiles;
8181
personalisationParameters?: Array<string>;
8282
proofingEnabled?: boolean;
83+
supplierReferences?: {
84+
[key: string]: string;
85+
};
8386
};
8487

8588
export type LetterType = 'q4' | 'x0' | 'x1';

lambdas/sftp-letters/src/__tests__/app/request-proof.test.ts

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const templateId = 'template-id';
2626
const templateName = 'template-name';
2727
const testDataVersionId = 'test-data-version-id';
2828

29-
const expandedTemplateId = `${clientId}_${campaignId}_${templateId}_${language}_${letterType}`;
29+
const supplierReference = `${clientId}_${campaignId}_${templateId}_${language}_${letterType}`;
3030

3131
function setup() {
3232
const userDataRepository = mock<UserDataRepository>();
@@ -97,7 +97,7 @@ describe('App', () => {
9797
const pdfContent = 'mock PDF content';
9898
const pdf = Readable.from(pdfContent);
9999

100-
const batchId = `${expandedTemplateId}-0000000000000_pdfversionid`;
100+
const batchId = `${supplierReference}-0000000000000_pdfversionid`;
101101

102102
const testData = [
103103
{ custom1: 'short1', custom2: 'short2' },
@@ -108,21 +108,21 @@ describe('App', () => {
108108
const batchData = [
109109
{
110110
clientRef: 'random1_random2_1744184100',
111-
template: expandedTemplateId,
111+
template: supplierReference,
112112
pdsField: 'pdsVal1',
113113
custom1: 'short1',
114114
custom2: 'short2',
115115
},
116116
{
117117
clientRef: 'random3_random4_1744184100',
118-
template: expandedTemplateId,
118+
template: supplierReference,
119119
pdsField: 'pdsVal2',
120120
custom1: 'medium1',
121121
custom2: 'medium2',
122122
},
123123
{
124124
clientRef: 'random5_random6_1744184100',
125-
template: expandedTemplateId,
125+
template: supplierReference,
126126
pdsField: 'pdsVal3',
127127
custom1: 'long1',
128128
custom2: 'long2',
@@ -147,15 +147,15 @@ describe('App', () => {
147147
const batchHash = 'hash-of-batch-csv';
148148

149149
const manifestData: Manifest = {
150-
template: expandedTemplateId,
150+
template: supplierReference,
151151
batch: `${batchId}.csv`,
152152
records: '3',
153153
md5sum: batchHash,
154154
};
155155

156156
const manifestCsv = [
157157
'template,batch,records,md5sum',
158-
`"${expandedTemplateId}","${batchId}.csv","3","${batchHash}"`,
158+
`"${supplierReference}","${batchId}.csv","3","${batchHash}"`,
159159
].join('\n');
160160

161161
const testDataCsv = mockTestDataCsv(['custom1', 'custom2'], testData);
@@ -181,7 +181,9 @@ describe('App', () => {
181181

182182
mocks.syntheticBatch.buildManifest.mockReturnValueOnce(manifestData);
183183

184-
mocks.templateRepository.acquireLock.mockResolvedValueOnce(true);
184+
mocks.templateRepository.acquireLockAndSetSupplierReference.mockResolvedValueOnce(
185+
true
186+
);
185187

186188
sftpClient.connect.mockResolvedValueOnce();
187189

@@ -211,7 +213,7 @@ describe('App', () => {
211213

212214
expect(mocks.syntheticBatch.buildBatch).toHaveBeenCalledTimes(1);
213215
expect(mocks.syntheticBatch.buildBatch).toHaveBeenCalledWith(
214-
expandedTemplateId,
216+
supplierReference,
215217
personalisationParameters,
216218
testData
217219
);
@@ -223,31 +225,32 @@ describe('App', () => {
223225

224226
expect(mocks.syntheticBatch.buildManifest).toHaveBeenCalledTimes(1);
225227
expect(mocks.syntheticBatch.buildManifest).toHaveBeenCalledWith(
226-
expandedTemplateId,
228+
supplierReference,
227229
batchId,
228230
batchCsv
229231
);
230232

231233
expect(sftpClient.connect).toHaveBeenCalledTimes(1);
232234

233-
expect(mocks.templateRepository.acquireLock).toHaveBeenCalledTimes(1);
234-
expect(mocks.templateRepository.acquireLock).toHaveBeenCalledWith(
235-
clientId,
236-
templateId
237-
);
235+
expect(
236+
mocks.templateRepository.acquireLockAndSetSupplierReference
237+
).toHaveBeenCalledTimes(1);
238+
expect(
239+
mocks.templateRepository.acquireLockAndSetSupplierReference
240+
).toHaveBeenCalledWith(clientId, templateId, supplier, supplierReference);
238241

239242
expect(sftpClient.exists).toHaveBeenCalledTimes(1);
240243
expect(sftpClient.exists).toHaveBeenCalledWith(
241-
`${baseUploadDir}/${sftpEnvironment}/batches/${expandedTemplateId}/${batchId}_MANIFEST.csv`
244+
`${baseUploadDir}/${sftpEnvironment}/batches/${supplierReference}/${batchId}_MANIFEST.csv`
242245
);
243246

244247
expect(sftpClient.mkdir).toHaveBeenCalledTimes(2);
245248
expect(sftpClient.mkdir).toHaveBeenCalledWith(
246-
`${baseUploadDir}/${sftpEnvironment}/templates/${expandedTemplateId}`,
249+
`${baseUploadDir}/${sftpEnvironment}/templates/${supplierReference}`,
247250
true
248251
);
249252
expect(sftpClient.mkdir).toHaveBeenCalledWith(
250-
`${baseUploadDir}/${sftpEnvironment}/batches/${expandedTemplateId}`,
253+
`${baseUploadDir}/${sftpEnvironment}/batches/${supplierReference}`,
251254
true
252255
);
253256

@@ -260,28 +263,28 @@ describe('App', () => {
260263

261264
expect(await streamToString(pdfArg)).toEqual(pdfContent);
262265
expect(pdfDestinationArg).toBe(
263-
`${baseUploadDir}/${sftpEnvironment}/templates/${expandedTemplateId}/${expandedTemplateId}.pdf`
266+
`${baseUploadDir}/${sftpEnvironment}/templates/${supplierReference}/${supplierReference}.pdf`
264267
);
265268

266269
const [batchArg, batchDestinationArg] = batchPutCall;
267270

268271
expect(await streamToString(batchArg)).toEqual(batchCsv);
269272
expect(batchDestinationArg).toBe(
270-
`${baseUploadDir}/${sftpEnvironment}/batches/${expandedTemplateId}/${batchId}.csv`
273+
`${baseUploadDir}/${sftpEnvironment}/batches/${supplierReference}/${batchId}.csv`
271274
);
272275

273276
const [manifestArg, manifestDestinationArg] = manifestPutCall;
274277

275278
expect(await streamToString(manifestArg)).toEqual(manifestCsv);
276279
expect(manifestDestinationArg).toBe(
277-
`${baseUploadDir}/${sftpEnvironment}/batches/${expandedTemplateId}/${batchId}_MANIFEST.csv`
280+
`${baseUploadDir}/${sftpEnvironment}/batches/${supplierReference}/${batchId}_MANIFEST.csv`
278281
);
279282

280283
expect(
281284
mocks.emailClient.sendProofRequestedEmailToSupplier
282285
).toHaveBeenCalledWith(
283286
templateId,
284-
expandedTemplateId,
287+
supplierReference,
285288
templateName,
286289
supplier
287290
);
@@ -330,28 +333,28 @@ describe('App', () => {
330333
const pdfContent = 'mock PDF content';
331334
const pdf = Readable.from(pdfContent);
332335

333-
const batchId = `${expandedTemplateId}-0000000000000_pdfversionid`;
336+
const batchId = `${supplierReference}-0000000000000_pdfversionid`;
334337

335338
const batchData = [
336339
{
337340
clientRef: 'random1_random2_1744184100',
338-
template: expandedTemplateId,
341+
template: supplierReference,
339342
pdsField: 'pdsVal1',
340343
},
341344
{
342345
clientRef: 'random3_random4_1744184100',
343-
template: expandedTemplateId,
346+
template: supplierReference,
344347
pdsField: 'pdsVal2',
345348
},
346349
{
347350
clientRef: 'random5_random6_1744184100',
348-
template: expandedTemplateId,
351+
template: supplierReference,
349352
pdsField: 'pdsVal3',
350353
},
351354
];
352355

353356
const manifestData: Manifest = {
354-
template: expandedTemplateId,
357+
template: supplierReference,
355358
batch: `${batchId}.csv`,
356359
records: '3',
357360
md5sum: 'hash-of-batch-csv',
@@ -376,7 +379,9 @@ describe('App', () => {
376379
sftpClient.connect.mockResolvedValueOnce();
377380

378381
// already locked
379-
mocks.templateRepository.acquireLock.mockResolvedValueOnce(false);
382+
mocks.templateRepository.acquireLockAndSetSupplierReference.mockResolvedValueOnce(
383+
false
384+
);
380385
sftpClient.end.mockResolvedValueOnce();
381386

382387
const res = await app.send(JSON.stringify(event), messageId);
@@ -395,11 +400,12 @@ describe('App', () => {
395400
expect(mocks.syntheticBatch.getHeader).toHaveBeenCalledTimes(1);
396401
expect(mocks.syntheticBatch.buildManifest).toHaveBeenCalledTimes(1);
397402

398-
expect(mocks.templateRepository.acquireLock).toHaveBeenCalledTimes(1);
399-
expect(mocks.templateRepository.acquireLock).toHaveBeenCalledWith(
400-
clientId,
401-
templateId
402-
);
403+
expect(
404+
mocks.templateRepository.acquireLockAndSetSupplierReference
405+
).toHaveBeenCalledTimes(1);
406+
expect(
407+
mocks.templateRepository.acquireLockAndSetSupplierReference
408+
).toHaveBeenCalledWith(clientId, templateId, supplier, supplierReference);
403409

404410
expect(sftpClient.connect).toHaveBeenCalled();
405411
expect(sftpClient.exists).not.toHaveBeenCalled();
@@ -429,23 +435,23 @@ describe('App', () => {
429435
const batchData = [
430436
{
431437
clientRef: 'random1_random2_1744184100',
432-
template: expandedTemplateId,
438+
template: supplierReference,
433439
pdsField: 'pdsVal1',
434440
},
435441
{
436442
clientRef: 'random3_random4_1744184100',
437-
template: expandedTemplateId,
443+
template: supplierReference,
438444
pdsField: 'pdsVal2',
439445
},
440446
{
441447
clientRef: 'random5_random6_1744184100',
442-
template: expandedTemplateId,
448+
template: supplierReference,
443449
pdsField: 'pdsVal3',
444450
},
445451
];
446452

447453
const manifestData: Manifest = {
448-
template: expandedTemplateId,
454+
template: supplierReference,
449455
batch: `${batchId}.csv`,
450456
records: '3',
451457
md5sum: 'hash-of-batch-csv',
@@ -469,7 +475,9 @@ describe('App', () => {
469475
});
470476

471477
// not already locked
472-
mocks.templateRepository.acquireLock.mockResolvedValueOnce(true);
478+
mocks.templateRepository.acquireLockAndSetSupplierReference.mockResolvedValueOnce(
479+
true
480+
);
473481

474482
sftpClient.connect.mockResolvedValueOnce();
475483

@@ -493,11 +501,13 @@ describe('App', () => {
493501
expect(mocks.syntheticBatch.getHeader).toHaveBeenCalledTimes(1);
494502
expect(mocks.syntheticBatch.buildManifest).toHaveBeenCalledTimes(1);
495503
expect(sftpClient.connect).toHaveBeenCalledTimes(1);
496-
expect(mocks.templateRepository.acquireLock).toHaveBeenCalledTimes(1);
504+
expect(
505+
mocks.templateRepository.acquireLockAndSetSupplierReference
506+
).toHaveBeenCalledTimes(1);
497507

498508
expect(sftpClient.exists).toHaveBeenCalledTimes(1);
499509
expect(sftpClient.exists).toHaveBeenCalledWith(
500-
`${baseUploadDir}/${sftpEnvironment}/batches/${expandedTemplateId}/${batchId}_MANIFEST.csv`
510+
`${baseUploadDir}/${sftpEnvironment}/batches/${supplierReference}/${batchId}_MANIFEST.csv`
501511
);
502512

503513
expect(mocks.templateRepository.finaliseLock).toHaveBeenCalledTimes(1);
@@ -518,7 +528,7 @@ describe('App', () => {
518528

519529
const event = mockEvent(true, personalisationParameters);
520530

521-
const batchId = `${expandedTemplateId}-0000000000000_pdfversionid`;
531+
const batchId = `${supplierReference}-0000000000000_pdfversionid`;
522532

523533
const sftpClient = mock<SftpClient>();
524534

@@ -552,7 +562,7 @@ describe('App', () => {
552562
expect.objectContaining({
553563
batchId,
554564
description: 'Failed to handle proofing request',
555-
expandedTemplateId,
565+
supplierReference,
556566
level: 'error',
557567
message: error.message,
558568
messageId,
@@ -569,7 +579,7 @@ describe('App', () => {
569579

570580
const event = mockEvent(true, personalisationParameters);
571581

572-
const batchId = `${expandedTemplateId}-0000000000000_pdfversionid`;
582+
const batchId = `${supplierReference}-0000000000000_pdfversionid`;
573583

574584
const sftpClient = mock<SftpClient>();
575585

@@ -588,12 +598,14 @@ describe('App', () => {
588598
mocks.syntheticBatch.buildBatch.mockReturnValueOnce([]);
589599
mocks.syntheticBatch.getHeader.mockReturnValueOnce('header');
590600
mocks.syntheticBatch.buildManifest.mockReturnValueOnce({
591-
template: expandedTemplateId,
601+
template: supplierReference,
592602
batch: `${batchId}.csv`,
593603
records: '3',
594604
md5sum: 'hash-of-batch-csv',
595605
});
596-
mocks.templateRepository.acquireLock.mockResolvedValueOnce(true);
606+
mocks.templateRepository.acquireLockAndSetSupplierReference.mockResolvedValueOnce(
607+
true
608+
);
597609
sftpClient.exists.mockResolvedValueOnce(false);
598610

599611
const err = new Error('sftp close err');
@@ -608,7 +620,7 @@ describe('App', () => {
608620
expect.objectContaining({
609621
batchId,
610622
description: 'Failed to close SFTP connection',
611-
expandedTemplateId,
623+
supplierReference,
612624
level: 'error',
613625
message: err.message,
614626
messageId,

0 commit comments

Comments
 (0)