Skip to content

Commit

Permalink
feat: simulate mosip returning aid
Browse files Browse the repository at this point in the history
  • Loading branch information
naftis committed Nov 4, 2024
1 parent 17c3b10 commit a9ee81d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
17 changes: 5 additions & 12 deletions packages/mosip-mock/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import Fastify from "fastify";
import * as crypto from "crypto";
import { env } from "./constants";
import { createAid, createNid } from "./random-identifiers";

type OpenCRVSEvent = {
id: string;
trackingId: string;
};

const createNid = async () => {
console.log("Creating NID...");
await new Promise((resolve) => setTimeout(resolve, 10000));
console.log("NID created!");

const hash = crypto.createHash("sha256");
return hash.digest("hex").substring(0, 16);
};

const sendNid = async ({
token,
eventId,
Expand All @@ -42,7 +33,7 @@ const sendNid = async ({
);
}

return response.json();
return response.text();
};

const app = Fastify();
Expand All @@ -60,7 +51,9 @@ app.post("/webhooks/opencrvs", {
}
);

return reply.status(202).send();
return reply.status(202).send({
aid: createAid(),
});
},
});

Expand Down
15 changes: 15 additions & 0 deletions packages/mosip-mock/src/random-identifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as crypto from "crypto";

export const createNid = async () => {
console.log("Creating NID...");
await new Promise((resolve) => setTimeout(resolve, 10000));
console.log("NID created!");

const hash = crypto.createHash("sha256");
return `NID-${hash.digest("hex").substring(0, 12).toUpperCase()}`;
};

export const createAid = () => {
const hash = crypto.createHash("sha256");
return `AID-${hash.digest("hex").substring(0, 12).toUpperCase()}`;
};
4 changes: 3 additions & 1 deletion packages/server/src/mosip-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ export const postRecord = async ({
);
}

return response;
return response.json() as Promise<{
aid: string;
}>;
};
34 changes: 34 additions & 0 deletions packages/server/src/opencrvs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,37 @@ export const rejectRegistration = (
},
headers,
});

export const upsertRegistrationIdentifier = (
{
eventId,
identifierType,
identifierValue,
}: {
eventId: string;
identifierType: string;
identifierValue: string;
},
{ headers }: { headers: Record<string, any> }
) =>
post({
query: /* GraphQL */ `
mutation upsertRegistrationIdentifier(
$eventId: ID!
$identifierType: String!
$identifierValue: String!
) {
upsertRegistrationIdentifier(
eventId: $eventId
identifierType: $identifierType
identifierValue: $identifierValue
)
}
`,
variables: {
eventId,
identifierType,
identifierValue,
},
headers,
});
2 changes: 1 addition & 1 deletion packages/server/src/webhooks/mosip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const mosipHandler = async (
{
trackingId,
registrationNumber,
childIdentifiers: [{ type: "NID", value: nid }],
childIdentifiers: [{ type: "MOSIP_NID", value: nid }],
},
{ headers: { Authorization: `Bearer ${token}` } }
);
Expand Down
12 changes: 11 additions & 1 deletion packages/server/src/webhooks/opencrvs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getTrackingId,
ValidRecord,
} from "@opencrvs/commons/types";
import * as opencrvs from "../opencrvs-api";

export const opencrvsRecordSchema = z
.object({
Expand Down Expand Up @@ -46,10 +47,19 @@ export const opencrvsHandler = async (

request.log.info({ trackingId }, "Received record from OpenCRVS");

await mosip.postRecord({
const { aid } = await mosip.postRecord({
event: { id: eventId, trackingId },
token,
});

await opencrvs.upsertRegistrationIdentifier(
{
eventId,
identifierType: "MOSIP_AID",
identifierValue: aid,
},
{ headers: { Authorization: `Bearer ${token}` } }
);

return reply.code(202).send();
};

0 comments on commit a9ee81d

Please sign in to comment.