diff --git a/src/db.ts b/src/db.ts index 3fb2d89fc8..648631e0ea 100644 --- a/src/db.ts +++ b/src/db.ts @@ -1041,10 +1041,13 @@ export const saveAccountOpeningRequestToBusinessId = async ( await redisClient.set(idKey, businessId); }; -export const getPersonIdByAccountOpeningRequest = async ( - accountOpeningRequestId: string +export const getCustomerIdByAccountOpeningRequest = async ( + accountOpeningRequestId: string, + customerType = CustomerType.PERSON ) => { - const key = `${process.env.MOCKSOLARIS_REDIS_PREFIX}:accountOpeningRequestId-personId:${accountOpeningRequestId}`; + const key = `${ + process.env.MOCKSOLARIS_REDIS_PREFIX + }:accountOpeningRequestId-${customerType.toLowerCase()}Id:${accountOpeningRequestId}`; return redisClient.get(key); }; diff --git a/src/routes/accountOpeningRequest.ts b/src/routes/accountOpeningRequest.ts index ef6d3fa097..a0e275c887 100644 --- a/src/routes/accountOpeningRequest.ts +++ b/src/routes/accountOpeningRequest.ts @@ -5,7 +5,7 @@ import moment from "moment"; import { getPerson, savePerson, - getPersonIdByAccountOpeningRequest, + getCustomerIdByAccountOpeningRequest, redlock, getBusiness, saveBusiness, @@ -13,6 +13,7 @@ import { saveAccountOpeningRequestToEntityId, } from "../db"; import { + AccountOpeningRequest, AccountOpeningRequestStatus, CustomerType, MockBusiness, @@ -116,16 +117,50 @@ export const retrieveAccountOpeningRequest = async ( res: Response ) => { const { id: accountOpeningRequestId } = req.params; + let accountOpeningRequest: AccountOpeningRequest; - const personId = await getPersonIdByAccountOpeningRequest( - accountOpeningRequestId - ); + try { + let customerType = CustomerType.PERSON; + let entityId: string; + let entity: MockPerson | MockBusiness; - const person = await getPerson(personId); + entityId = await getCustomerIdByAccountOpeningRequest( + accountOpeningRequestId, + CustomerType.PERSON + ); - const accountOpeningRequest = person.accountOpeningRequests.find( - (request) => request.id === accountOpeningRequestId - ); + if (entityId) { + entity = await getPerson(entityId); + } else { + customerType = CustomerType.BUSINESS; + entityId = await getCustomerIdByAccountOpeningRequest( + accountOpeningRequestId, + CustomerType.BUSINESS + ); + entity = await getBusiness(entityId); + } + + if (!entity) { + throw new Error("Entity not found"); + } + + accountOpeningRequest = entity?.accountOpeningRequests.find( + (request) => request.id === accountOpeningRequestId + ); + } catch (err) { + res.status(HttpStatusCodes.NOT_FOUND).send({ + id: generateID(), + status: HttpStatusCodes.NOT_FOUND, + code: "not_found", + title: "Not Found", + detail: `Account Opening Request with id: ${accountOpeningRequestId} not found`, + source: { + message: `Account Opening Request with id: ${accountOpeningRequestId} not found`, + field: "id", + }, + }); + return; + } res.status(HttpStatusCodes.OK).send(accountOpeningRequest); }; diff --git a/tests/routes/accountOpeningRequest.spec.ts b/tests/routes/accountOpeningRequest.spec.ts index d900d1fe16..2fc05dd724 100644 --- a/tests/routes/accountOpeningRequest.spec.ts +++ b/tests/routes/accountOpeningRequest.spec.ts @@ -100,7 +100,7 @@ describe("Account Opening Request", () => { }); describe("retrieve Account Opening Request", () => { - let accountOpeningRequestId; + let accountOpeningRequestId: string; before(async () => { res = mockRes(); @@ -161,4 +161,28 @@ describe("Account Opening Request", () => { expect(args.payload.account_id).to.be.ok; }); }); + + describe("retrieve Account Opening Request (Business)", () => { + let accountOpeningRequestId: string; + + before(async () => { + res = mockRes(); + + accountOpeningRequestId = (await db.getBusiness(businessId)) + .accountOpeningRequests[0].id; + + const req = mockReq({ + params: { + id: accountOpeningRequestId, + }, + }); + + await accountOpeningRequestAPI.retrieveAccountOpeningRequest(req, res); + }); + + it("should return successful response", () => { + expect(res.status.calledWith(200)).to.be.true; + expect(res.send.getCall(0).args[0].id).to.equal(accountOpeningRequestId); + }); + }); });