-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters