Skip to content

Commit 86d9579

Browse files
committed
handle missing clientId in template-client
1 parent cc825ac commit 86d9579

File tree

2 files changed

+177
-14
lines changed

2 files changed

+177
-14
lines changed

lambdas/backend-api/src/__tests__/templates/app/template-client.test.ts

Lines changed: 165 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('templateClient', () => {
106106
});
107107
});
108108

109-
test('should return a failure result client configuration unexpectedly cant be fetched', async () => {
109+
test('should return a failure result when client configuration unexpectedly cant be fetched', async () => {
110110
const { templateClient, mocks } = setup();
111111

112112
const data: CreateUpdateTemplate = {
@@ -132,6 +132,47 @@ describe('templateClient', () => {
132132
});
133133
});
134134

135+
test('client configuration is not fetched if user has no clientId', async () => {
136+
const { templateClient, mocks } = setup();
137+
138+
const data: CreateUpdateTemplate = {
139+
templateType: 'EMAIL',
140+
name: 'name',
141+
message: 'message',
142+
subject: 'subject',
143+
};
144+
145+
const expectedTemplateDto: TemplateDto = {
146+
...data,
147+
id: templateId,
148+
createdAt: new Date().toISOString(),
149+
updatedAt: new Date().toISOString(),
150+
templateStatus: 'NOT_YET_SUBMITTED',
151+
};
152+
153+
const template: DatabaseTemplate = {
154+
...expectedTemplateDto,
155+
owner: user.userId,
156+
campaignId: 'campaignId',
157+
version: 1,
158+
};
159+
160+
mocks.templateRepository.create.mockResolvedValueOnce({
161+
data: template,
162+
});
163+
164+
const result = await templateClient.createTemplate(data, {
165+
userId: user.userId,
166+
clientId: undefined,
167+
});
168+
169+
expect(mocks.clientConfigRepository.get).not.toHaveBeenCalled();
170+
171+
expect(result).toEqual({
172+
data: expectedTemplateDto,
173+
});
174+
});
175+
135176
test('should return a failure result, when saving to the database unexpectedly fails', async () => {
136177
const { templateClient, mocks } = setup();
137178

@@ -392,6 +433,84 @@ describe('templateClient', () => {
392433
);
393434
});
394435

436+
test('if user has no clientId, client configuration is not fetched', async () => {
437+
const { templateClient, mocks } = setup();
438+
439+
const pdfFilename = 'template.pdf';
440+
441+
const data: CreateUpdateTemplate = {
442+
templateType: 'LETTER',
443+
name: 'name',
444+
language: 'en',
445+
letterType: 'x0',
446+
};
447+
448+
const pdf = new File(['pdf'], pdfFilename, {
449+
type: 'application/pdf',
450+
});
451+
452+
const filesWithVerions: LetterFiles = {
453+
pdfTemplate: {
454+
fileName: pdfFilename,
455+
currentVersion: versionId,
456+
virusScanStatus: 'PENDING',
457+
},
458+
proofs: {},
459+
};
460+
461+
const dataWithFiles: CreateUpdateTemplate & { files: LetterFiles } = {
462+
templateType: 'LETTER',
463+
name: 'name',
464+
language: 'en',
465+
letterType: 'x0',
466+
files: filesWithVerions,
467+
};
468+
469+
const creationTime = '2025-03-12T08:41:08.805Z';
470+
471+
const initialCreatedTemplate: DatabaseTemplate = {
472+
...dataWithFiles,
473+
id: templateId,
474+
createdAt: creationTime,
475+
updatedAt: creationTime,
476+
templateStatus: 'PENDING_UPLOAD',
477+
owner: user.userId,
478+
version: 1,
479+
};
480+
481+
const updateTime = '2025-03-12T08:41:33.666Z';
482+
483+
const finalTemplate: DatabaseTemplate = {
484+
...initialCreatedTemplate,
485+
templateStatus: 'PENDING_VALIDATION',
486+
updatedAt: updateTime,
487+
};
488+
489+
const { owner: _1, version: _2, ...expectedDto } = finalTemplate;
490+
491+
mocks.templateRepository.create.mockResolvedValueOnce({
492+
data: initialCreatedTemplate,
493+
});
494+
495+
mocks.letterUploadRepository.upload.mockResolvedValueOnce({ data: null });
496+
497+
mocks.templateRepository.updateStatus.mockResolvedValueOnce({
498+
data: finalTemplate,
499+
});
500+
501+
const result = await templateClient.createLetterTemplate(
502+
data,
503+
{ userId: user.userId, clientId: undefined },
504+
pdf
505+
);
506+
507+
expect(result).toEqual({
508+
data: expectedDto,
509+
});
510+
511+
expect(mocks.clientConfigRepository.get).not.toHaveBeenCalled();
512+
});
513+
395514
test('should return a failure result, when template data is invalid', async () => {
396515
const { templateClient, mocks } = setup();
397516

@@ -1700,6 +1819,30 @@ describe('templateClient', () => {
17001819
});
17011820
});
17021821

1822+
test('should return a failure result, when user has no clientId (so proofing cannot be determined to be enabled)', async () => {
1823+
const { templateClient, mocks } = setup();
1824+
1825+
const result = await templateClient.requestProof(templateId, {
1826+
userId: user.userId,
1827+
clientId: undefined,
1828+
});
1829+
1830+
expect(mocks.clientConfigRepository.get).not.toHaveBeenCalled();
1831+
1832+
expect(
1833+
mocks.templateRepository.proofRequestUpdate
1834+
).not.toHaveBeenCalled();
1835+
1836+
expect(mocks.queueMock.send).not.toHaveBeenCalled();
1837+
1838+
expect(result).toEqual({
1839+
error: {
1840+
code: 403,
1841+
message: 'User cannot request a proof',
1842+
},
1843+
});
1844+
});
1845+
17031846
test('should return updated template', async () => {
17041847
const { templateClient, mocks } = setup();
17051848

@@ -1822,7 +1965,7 @@ describe('templateClient', () => {
18221965
describe('getClientConfiguration', () => {
18231966
const clientId = 'client1';
18241967

1825-
test('should return a 404 failure result, when client configuration is not available', async () => {
1968+
test('should return a 404 failure result, when client configuration is not available for client', async () => {
18261969
const { templateClient, mocks } = setup();
18271970

18281971
mocks.clientConfigRepository.get.mockResolvedValueOnce({ data: null });
@@ -1842,6 +1985,26 @@ describe('templateClient', () => {
18421985
});
18431986
});
18441987

1988+
test('should return a 404 failure result, when user has no clientId', async () => {
1989+
const { templateClient, mocks } = setup();
1990+
1991+
mocks.clientConfigRepository.get.mockResolvedValueOnce({ data: null });
1992+
1993+
const result = await templateClient.getClientConfiguration({
1994+
clientId: undefined,
1995+
userId: 'sub',
1996+
});
1997+
1998+
expect(mocks.clientConfigRepository.get).not.toHaveBeenCalled();
1999+
2000+
expect(result).toEqual({
2001+
error: {
2002+
code: 404,
2003+
message: 'Client configuration is not available',
2004+
},
2005+
});
2006+
});
2007+
18452008
test('should return a failure result, when client configuration unexpectedly cannot be fetched', async () => {
18462009
const { templateClient, mocks } = setup();
18472010

lambdas/backend-api/src/templates/app/template-client.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ export class TemplateClient {
4949
return validationResult;
5050
}
5151

52-
const clientConfigurationResult = await this.clientConfigRepository.get(
53-
String(user.clientId)
54-
);
52+
const clientConfigurationResult = user.clientId
53+
? await this.clientConfigRepository.get(user.clientId)
54+
: { data: null };
5555

5656
if (clientConfigurationResult.error) {
5757
log.error('Failed to fetch client configuration', {
@@ -146,9 +146,9 @@ export class TemplateClient {
146146
files,
147147
};
148148

149-
const clientConfigurationResult = await this.clientConfigRepository.get(
150-
String(user.clientId)
151-
);
149+
const clientConfigurationResult = user.clientId
150+
? await this.clientConfigRepository.get(user.clientId)
151+
: { data: null };
152152

153153
if (clientConfigurationResult.error) {
154154
log.error('Failed to fetch client configuration', {
@@ -351,9 +351,9 @@ export class TemplateClient {
351351
): Promise<Result<TemplateDto>> {
352352
const log = this.logger.child({ templateId, user });
353353

354-
const clientConfigurationResult = await this.clientConfigRepository.get(
355-
String(user.clientId)
356-
);
354+
const clientConfigurationResult = user.clientId
355+
? await this.clientConfigRepository.get(user.clientId)
356+
: { data: null };
357357

358358
if (clientConfigurationResult.error) {
359359
log.error('Failed to fetch client configuration', {
@@ -465,9 +465,9 @@ export class TemplateClient {
465465
user,
466466
});
467467

468-
const clientConfigurationResult = await this.clientConfigRepository.get(
469-
String(user.clientId)
470-
);
468+
const clientConfigurationResult = user.clientId
469+
? await this.clientConfigRepository.get(user.clientId)
470+
: { data: null };
471471

472472
if (clientConfigurationResult.error) {
473473
log.error('Failed to fetch client configuration', {

0 commit comments

Comments
 (0)