Skip to content

Commit

Permalink
Retrieve account opening request for business
Browse files Browse the repository at this point in the history
  • Loading branch information
p-janik committed Nov 4, 2024
1 parent 039407c commit bc53ecd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
51 changes: 43 additions & 8 deletions src/routes/accountOpeningRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import moment from "moment";
import {
getPerson,
savePerson,
getPersonIdByAccountOpeningRequest,
getCustomerIdByAccountOpeningRequest,
redlock,
getBusiness,
saveBusiness,
saveAccountOpeningRequestToBusinessId,
saveAccountOpeningRequestToEntityId,
} from "../db";
import {
AccountOpeningRequest,
AccountOpeningRequestStatus,
CustomerType,
MockBusiness,
Expand Down Expand Up @@ -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);
};
26 changes: 25 additions & 1 deletion tests/routes/accountOpeningRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("Account Opening Request", () => {
});

describe("retrieve Account Opening Request", () => {
let accountOpeningRequestId;
let accountOpeningRequestId: string;

before(async () => {
res = mockRes();
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit bc53ecd

Please sign in to comment.