Skip to content

Commit 6c4f23f

Browse files
authored
fix: No account/entity handling (#104)
* fix: add return for no account/entity * refactor: changed handler function name * fix: handling of logic return data and log statement * test: added code for the result of logic
1 parent 57cdaa7 commit 6c4f23f

File tree

5 files changed

+219
-187
lines changed

5 files changed

+219
-187
lines changed

__tests__/unit/app.eventFlow.test.ts

+29-21
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ describe('getConditionForEntity', () => {
306306
jest.spyOn(databaseManager, 'set').mockImplementation(() => {
307307
return Promise.resolve(undefined);
308308
});
309+
310+
jest.spyOn(databaseManager, 'getEntity').mockImplementation(() => {
311+
return Promise.resolve([[{ _id: 'entity456' }]]);
312+
});
309313
});
310314

311315
it('should get conditions for entity', async () => {
@@ -320,7 +324,16 @@ describe('getConditionForEntity', () => {
320324
});
321325
const result = await handleGetConditionsForEntity({ id: '', schmenm: '', synccache: 'no' });
322326
// Assert
323-
expect(result).toEqual(undefined);
327+
expect(result).toEqual({ code: 404 });
328+
});
329+
330+
it('should get no entity was found', async () => {
331+
jest.spyOn(databaseManager, 'getEntity').mockImplementation(() => {
332+
return Promise.resolve([]);
333+
});
334+
const result = await handleGetConditionsForEntity({ id: '', schmenm: '', synccache: 'no' });
335+
// Assert
336+
expect(result).toEqual({ result: 'Entity does not exist in the database', code: 404 });
324337
});
325338

326339
it('should get conditions for entity and update cache', async () => {
@@ -354,15 +367,6 @@ describe('getConditionForEntity', () => {
354367
// Assert
355368
expect(result).toEqual(entityResponse);
356369
});
357-
358-
it('should throw an error', async () => {
359-
jest.spyOn(databaseManager, 'getEntityConditionsByGraph').mockImplementation(() => {
360-
return Promise.reject(new Error('something bad happened'));
361-
});
362-
const result = await handleGetConditionsForEntity({ id: '', schmenm: '' });
363-
364-
expect(result).toBe(undefined);
365-
});
366370
});
367371

368372
describe('handlePostConditionAccount', () => {
@@ -575,6 +579,10 @@ describe('getConditionForAccount', () => {
575579
beforeEach(() => {
576580
jest.clearAllMocks(); // Clear mocks before each test
577581

582+
jest.spyOn(databaseManager, 'getAccount').mockImplementation(() => {
583+
return Promise.resolve([[{ _id: 'account456' }]]);
584+
});
585+
578586
jest.spyOn(databaseManager, 'getAccountConditionsByGraph').mockImplementation(() => {
579587
return Promise.resolve([[rawResponseAccount]]);
580588
});
@@ -599,7 +607,16 @@ describe('getConditionForAccount', () => {
599607
const result = await handleGetConditionsForAccount({ id: '1010101010', synccache: 'no', schmenm: 'Mxx', agt: 'dfsp001' });
600608

601609
// Assert
602-
expect(result).toEqual(undefined);
610+
expect(result).toEqual({ code: 404 });
611+
});
612+
613+
it('should get no account was found', async () => {
614+
jest.spyOn(databaseManager, 'getAccount').mockImplementation(() => {
615+
return Promise.resolve([]);
616+
});
617+
const result = await handleGetConditionsForAccount({ id: '1010101010', synccache: 'no', schmenm: 'Mxx', agt: 'dfsp001' });
618+
// Assert
619+
expect(result).toEqual({ result: 'Account does not exist in the database', code: 404 });
603620
});
604621

605622
it('should get conditions for account and update cache', async () => {
@@ -645,15 +662,6 @@ describe('getConditionForAccount', () => {
645662
// Assert
646663
expect(result).toEqual(accountResponse);
647664
});
648-
649-
it('should throw an error', async () => {
650-
jest.spyOn(databaseManager, 'getAccountConditionsByGraph').mockImplementation(() => {
651-
return Promise.reject(new Error('something bad happened'));
652-
});
653-
const result = await handleGetConditionsForAccount({ id: '', schmenm: '', agt: '002', synccache: 'no' });
654-
655-
expect(result).toBe(undefined);
656-
});
657665
});
658666

659667
describe('handleUpdateExpiryDateForConditionsOfAccount', () => {
@@ -986,6 +994,6 @@ describe('handleCacheUpdate', () => {
986994

987995
const result = await handleRefreshCache(true, 12);
988996

989-
expect(result).toBeUndefined();
997+
expect(result).toBe(undefined);
990998
});
991999
});

__tests__/unit/test.data.ts

+69-63
Original file line numberDiff line numberDiff line change
@@ -186,78 +186,84 @@ export const rawResponseAccount = {
186186
};
187187

188188
export const accountResponse = {
189-
acct: {
190-
id: '1010101010',
191-
agt: {
192-
finInstnId: {
193-
clrSysMmbId: {
194-
mmbId: 'dfsp001',
189+
code: 200,
190+
result: {
191+
acct: {
192+
id: '1010101010',
193+
agt: {
194+
finInstnId: {
195+
clrSysMmbId: {
196+
mmbId: 'dfsp001',
197+
},
195198
},
196199
},
200+
schmeNm: {
201+
prtry: 'Mxx',
202+
},
197203
},
198-
schmeNm: {
199-
prtry: 'Mxx',
200-
},
204+
conditions: [
205+
{
206+
condId: '2110',
207+
xprtnDtTm,
208+
condTp: 'overridable-block',
209+
creDtTm: fixedDate,
210+
incptnDtTm,
211+
condRsn: 'R001',
212+
usr: 'bob',
213+
prsptvs: [
214+
{
215+
prsptv: 'governed_as_creditor_account_by',
216+
evtTp: ['pacs.008.001.10'],
217+
incptnDtTm,
218+
xprtnDtTm,
219+
},
220+
{
221+
prsptv: 'governed_as_debtor_account_by',
222+
evtTp: ['pacs.008.001.10'],
223+
incptnDtTm,
224+
xprtnDtTm,
225+
},
226+
],
227+
},
228+
],
201229
},
202-
conditions: [
203-
{
204-
condId: '2110',
205-
xprtnDtTm,
206-
condTp: 'overridable-block',
207-
creDtTm: fixedDate,
208-
incptnDtTm,
209-
condRsn: 'R001',
210-
usr: 'bob',
211-
prsptvs: [
212-
{
213-
prsptv: 'governed_as_creditor_account_by',
214-
evtTp: ['pacs.008.001.10'],
215-
incptnDtTm,
216-
xprtnDtTm,
217-
},
218-
{
219-
prsptv: 'governed_as_debtor_account_by',
220-
evtTp: ['pacs.008.001.10'],
221-
incptnDtTm,
222-
xprtnDtTm,
223-
},
224-
],
225-
},
226-
],
227230
};
228231

229232
export const entityResponse = {
230-
ntty: {
231-
id: '+27733161225',
232-
schmeNm: {
233-
prtry: 'MSISDN',
233+
code: 200,
234+
result: {
235+
ntty: {
236+
id: '+27733161225',
237+
schmeNm: {
238+
prtry: 'MSISDN',
239+
},
234240
},
241+
conditions: [
242+
{
243+
condId: '2110',
244+
condTp: 'overridable-block',
245+
incptnDtTm,
246+
xprtnDtTm,
247+
condRsn: 'R001',
248+
usr: 'bob',
249+
creDtTm: fixedDate,
250+
prsptvs: [
251+
{
252+
prsptv: 'governed_as_creditor_by',
253+
evtTp: ['pacs.008.001.10'],
254+
incptnDtTm,
255+
xprtnDtTm,
256+
},
257+
{
258+
prsptv: 'governed_as_debtor_by',
259+
evtTp: ['pacs.008.001.10'],
260+
incptnDtTm,
261+
xprtnDtTm,
262+
},
263+
],
264+
},
265+
],
235266
},
236-
conditions: [
237-
{
238-
condId: '2110',
239-
condTp: 'overridable-block',
240-
incptnDtTm,
241-
xprtnDtTm,
242-
condRsn: 'R001',
243-
usr: 'bob',
244-
creDtTm: fixedDate,
245-
prsptvs: [
246-
{
247-
prsptv: 'governed_as_creditor_by',
248-
evtTp: ['pacs.008.001.10'],
249-
incptnDtTm,
250-
xprtnDtTm,
251-
},
252-
{
253-
prsptv: 'governed_as_debtor_by',
254-
evtTp: ['pacs.008.001.10'],
255-
incptnDtTm,
256-
xprtnDtTm,
257-
},
258-
],
259-
},
260-
],
261267
};
262268

263269
export const sampleEntityCondition: EntityCondition = {

src/app.controller.ts

+20-24
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const reportRequestHandler = async (req: FastifyRequest, reply: FastifyRe
3636
};
3737

3838
export const postConditionHandlerEntity = async (req: FastifyRequest, reply: FastifyReply): Promise<void> => {
39-
loggerService.log('Start - Handle report request');
39+
loggerService.log('Start - Handle saving entity condition request');
4040
try {
4141
const condition = req.body as EntityCondition;
4242
const data = await handlePostConditionEntity(condition);
@@ -88,44 +88,40 @@ export const handleHealthCheck = async (): Promise<{ status: string }> => {
8888
};
8989
};
9090

91-
export const getConditionHandler = async (req: FastifyRequest, reply: FastifyReply): Promise<void> => {
91+
export const getEntityConditionHandler = async (req: FastifyRequest, reply: FastifyReply): Promise<void> => {
9292
loggerService.trace('getting conditions for an entity');
9393
try {
94-
const data = await handleGetConditionsForEntity(req.query as ConditionRequest);
95-
if (data) {
96-
reply.status(200);
97-
reply.send(data);
98-
} else {
99-
reply.status(404);
100-
}
94+
const { code, result } = await handleGetConditionsForEntity(req.query as ConditionRequest);
95+
96+
reply.status(code);
97+
reply.send(result);
10198
} catch (err) {
99+
loggerService.error(err as Error);
102100
reply.status(500);
101+
reply.send(err);
103102
} finally {
104103
loggerService.trace('End - get condition for an entity');
105104
}
106105
};
107106

108107
export const getAccountConditionsHandler = async (req: FastifyRequest, reply: FastifyReply): Promise<void> => {
109-
loggerService.log('Start - Handle report request');
108+
loggerService.log('Start - Handle get account condition request');
110109
try {
111-
const data = await handleGetConditionsForAccount(req.query as ConditionRequest);
112-
if (data) {
113-
reply.status(200);
114-
reply.send(data);
115-
} else {
116-
reply.status(404);
117-
}
110+
const { code, result } = await handleGetConditionsForAccount(req.query as ConditionRequest);
111+
112+
reply.status(code);
113+
reply.send(result);
118114
} catch (err) {
119-
const failMessage = `Failed to process execution request. \n${JSON.stringify(err, null, 4)}`;
115+
loggerService.error(err as Error);
120116
reply.status(500);
121-
reply.send(failMessage);
117+
reply.send(err);
122118
} finally {
123-
loggerService.log('End - Handle report request');
119+
loggerService.log('End - Handle get account condition request');
124120
}
125121
};
126122

127123
export const updateAccountConditionExpiryDateHandler = async (req: FastifyRequest, reply: FastifyReply): Promise<void> => {
128-
loggerService.log('Start - Handle update condition request');
124+
loggerService.log('Start - Handle update condition for account request');
129125
const expiryDate = (req.body as { xprtnDtTm?: string })?.xprtnDtTm;
130126
try {
131127
const { code, message } = await handleUpdateExpiryDateForConditionsOfAccount(req.query as ConditionRequest, expiryDate);
@@ -136,12 +132,12 @@ export const updateAccountConditionExpiryDateHandler = async (req: FastifyReques
136132
} catch (err) {
137133
reply.send(err);
138134
} finally {
139-
loggerService.log('End - Handle update condition request');
135+
loggerService.log('End - Handle update condition for account request');
140136
}
141137
};
142138

143139
export const updateEntityConditionExpiryDateHandler = async (req: FastifyRequest, reply: FastifyReply): Promise<void> => {
144-
loggerService.log('Start - Handle update condition request');
140+
loggerService.log('Start - Handle update condition for entity request');
145141
const expiryDate = (req.body as { xprtnDtTm?: string })?.xprtnDtTm;
146142
try {
147143
const { code, message } = await handleUpdateExpiryDateForConditionsOfEntity(req.query as ConditionRequest, expiryDate);
@@ -152,6 +148,6 @@ export const updateEntityConditionExpiryDateHandler = async (req: FastifyRequest
152148
} catch (err) {
153149
reply.send(err);
154150
} finally {
155-
loggerService.log('End - Handle update condition request');
151+
loggerService.log('End - Handle update condition for entity request');
156152
}
157153
};

0 commit comments

Comments
 (0)