Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanyvu committed Jan 21, 2025
1 parent 7771b76 commit 83e96f9
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 11 deletions.
13 changes: 13 additions & 0 deletions lib/lambda/submit/splitSPAId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { search } from "libs/opensearch-lib";
import { getDomainAndNamespace } from "libs/utils";

export const getNextSplitSPAId = async (spaId: string) => {
const { domain, index } = getDomainAndNamespace("main");
const query = {
regexp: {
id: `${spaId}-[a-zA-Z]`,
},
};
const matchingPackages = search(domain, index, query);
console.log(matchingPackages, "HELLOOO");
};
78 changes: 78 additions & 0 deletions lib/lambda/submit/submitSplitSPA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { response } from "libs/handler-lib";
import { APIGatewayEvent } from "aws-lambda";
import { getPackage } from "libs/api/package";
// import { produceMessage } from "libs/api/kafka";
import { ItemResult } from "shared-types/opensearch/main";
import { getNextSplitSPAId } from "./splitSPAId";
import { z } from "zod";

const sendSubmitSplitSPAMessage = async (currentPackage: ItemResult) => {
const topicName = process.env.topicName as string;
if (!topicName) {
throw new Error("Topic name is not defined");
}
getNextSplitSPAId(currentPackage._id);
// ID and changeMade are excluded; the rest of the object has to be spread into the new package
// const {
// id: _id,
// changeMade: _changeMade,
// origin: _origin,
// ...remainingFields
// } = currentPackage._source;

// await produceMessage(
// topicName,
// updatedId, // CHANGE TO ADD ALPHABET
// JSON.stringify({
// id: updatedId, // CHANGE TO ADD ALPHABET
// idToBeUpdated: currentPackage._id,
// ...remainingFields,
// origin: "OneMAC",
// changeMade: "ID has been updated.",
// isAdminChange: true,
// adminChangeType: "update-id",
// }),
// );
};

const splitSPAEventBodySchema = z.object({
packageId: z.string(),
});

export const handler = async (event: APIGatewayEvent) => {
if (!event.body) {
return response({
statusCode: 400,
body: { message: "Event body required" },
});
}

try {
const { packageId } = splitSPAEventBodySchema.parse(
event.body === "string" ? JSON.parse(event.body) : event.body,
);

if (!packageId) {
return response({
statusCode: 400,
body: { message: "Package ID to split is required" },
});
}

const currentPackage = await getPackage(packageId);
if (!currentPackage || currentPackage.found == false) {
return response({
statusCode: 404,
body: { message: "No record found for the given id" },
});
}

return await sendSubmitSplitSPAMessage(currentPackage);
} catch (err) {
console.error("Error has occured modifying package:", err);
return response({
statusCode: 500,
body: { message: err.message || "Internal Server Error" },
});
}
};
6 changes: 6 additions & 0 deletions lib/lambda/update/adminChangeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export const updateIdAdminChangeSchema = z
})
.and(z.record(z.string(), z.any()));

// TODO
export const splitSPAAdminChangeSchema = z.object({
id: z.string(),
adminChangeType: z.literal("split-spa"),
});

export const transformDeleteSchema = (offset: number) =>
deleteAdminChangeSchema.transform((data) => ({
...data,
Expand Down
12 changes: 1 addition & 11 deletions lib/lambda/update/updatePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,43 +94,33 @@ const sendUpdateIdMessage = async ({
if (!topicName) {
throw new Error("Topic name is not defined");
}
console.log("AM I IN HERE");
// ID and changeMade are excluded; the rest of the object has to be spread into the new package
const {
id: _id,
changeMade: _changeMade,
origin: _origin,
...remainingFields
} = currentPackage._source;
console.log(currentPackage, "CURRENT PACKAGE??");
if (updatedId === currentPackage._id) {
return response({
statusCode: 400,
body: { message: "New ID required to update package" },
});
}
console.log("BEFORE");
console.log(updatedId, "UPDATED ID IN FUNC??");
// check if a package with this new ID already exists
const packageExists = await getPackage(updatedId);
console.log(packageExists, "WHAT IS THIS");
if (packageExists) {
return response({
statusCode: 400,
body: { message: "This ID already exists" },
});
}
console.log("AFTER HERE");
// use event of current package to determine how ID should be formatted
const packageEvent = await getPackageType(currentPackage._id);
console.log(packageEvent, "PACKAGE EVENT?");
const packageSubmissionTypeSchema = events[packageEvent as keyof typeof events].baseSchema;
console.log(packageSubmissionTypeSchema, "SCHEMA???");

const idSchema = packageSubmissionTypeSchema.shape.id;
console.log(idSchema, "ID SCHEMA???");
const parsedId = idSchema.safeParse(updatedId);
console.log(parsedId, "PARSED IDDD");

if (!parsedId.success) {
return response({
Expand All @@ -140,7 +130,7 @@ const sendUpdateIdMessage = async ({
}

await sendDeleteMessage(currentPackage._id);
console.log("JUST DELETED");

await produceMessage(
topicName,
updatedId,
Expand Down
1 change: 1 addition & 0 deletions lib/libs/api/package/getPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ExtendedItemResult extends ItemResult {
}
export const getPackage = async (id: string): Promise<ItemResult | undefined> => {
const { domain, index } = getDomainAndNamespace("main");

const packageResult = await os.getItem(domain, index, id);

return packageResult;
Expand Down
11 changes: 11 additions & 0 deletions lib/stacks/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ export class Api extends cdk.NestedStack {
indexNamespace,
},
},
{
id: "submitSplitSPA",
entry: join(__dirname, "../lambda/submit/submitSplitSPA.ts"),
environment: {
topicName,
brokerString,
osDomain: `https://${openSearchDomainEndpoint}`,
indexNamespace,
},
provisionedConcurrency: 2,
},
];

const lambdas = lambdaDefinitions.reduce(
Expand Down

0 comments on commit 83e96f9

Please sign in to comment.