Skip to content

Commit

Permalink
adding attachments to change log
Browse files Browse the repository at this point in the history
  • Loading branch information
andieswift committed Jan 16, 2025
1 parent a605979 commit 54d7968
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 63 deletions.
Empty file modified bin/cli/src/run.ts
100644 → 100755
Empty file.
6 changes: 6 additions & 0 deletions lib/lambda/sinkChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./update/adminChangeSchemas";
import { transformSubmitValuesSchema } from "./submit/submitNOSO";
import { getPackageChangelog } from "lib/libs/api/package";
import { copyAttachments } from "./submit/submitNOSO";

// One notable difference between this handler and sinkMain's...
// The order in which records are processed for the changelog doesn't matter.
Expand Down Expand Up @@ -75,6 +76,8 @@ const processAndIndex = async ({

const result = schema.safeParse(record);

// attachments function

if (result.success) {
if (result.data.adminChangeType === "update-id") {
docs.forEach((log) => {
Expand All @@ -97,6 +100,9 @@ const processAndIndex = async ({
packageId: result.data.id,
});
});
} else if (result.data.adminChangeType === "NOSO") {
const data = copyAttachments(result.data);
docs.push(data);
} else {
docs.push(result.data);
}
Expand Down
120 changes: 60 additions & 60 deletions lib/lambda/submit/submitNOSO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,50 @@ import { ItemResult } from "shared-types/opensearch/main";
export const submitNOSOAdminSchema = z
.object({
packageId: z.string(),
adminChangeType: z.literal("NOSO"),
action: z.literal("NOSO"),
copyAttachmentsFrom: z.string().optional(),
changeIdTo: z.string().optional(),
})
.and(z.record(z.string(), z.any()));

export const transformSubmitValuesSchema = submitNOSOAdminSchema.transform((data) => ({
...data,
adminChangeType: "NOSO",
event: "NOSO",
id: data.packageId,
packageId: data.packageId,
timestamp: Date.now(),
copyAttachmentsFromId: z.string().optional(),
}));

const sendSubmitMessage = async ({
packageId,
currentPackage,
}: {
packageId: string;
currentPackage: ItemResult;
}) => {
const topicName = process.env.topicName as string;
if (!topicName) {
throw new Error("Topic name is not defined");
}
await produceMessage(
topicName,
packageId,
JSON.stringify({
...currentPackage._source,
event: "legacy-admin-change", // this is added to ensure the frontend looks as it should
origin: "SEATool",
isAdminChange: true,
adminChangeType: "NOSO",
changeMade: `${packageId} added to OneMAC. Package not originally submitted in OneMAC. At this time, the attachments for this package are unavailable in this system. Contact your CPOC to verify the initial submission documents.`,
changeReason: `This is a Not Originally Submitted in OneMAC (NOSO) that users need to see in OneMAC.`,
}),
);

return response({
statusCode: 200,
body: { message: `${packageId} has been submitted.` },
});
};
export const copyAttachments = async (data: any) => {
//ANDIE: change type not any
// change any
if (!data.copyAttachmentsFromId) return data;
const copyAttachmentsFromId = data.copyAttachments;

const copyAttachments = async ({
currentPackage,
copyAttachmentsFromId,
}: {
currentPackage: ItemResult;
copyAttachmentsFromId: string;
}) => {
// get the attachementPackage
const attachPackage = await getPackage(copyAttachmentsFromId);
const attachPackageChangelog = await getPackageChangelog(copyAttachmentsFromId);

const currentPackage = await getPackage(data.id);
const currentPackageChangelog = await getPackageChangelog(data.id);

if (!attachPackage || attachPackage.found == false) {
console.error(`Copy Attachment Package of id: ${copyAttachmentsFromId} not found`);
return;
return data;
}

if (!currentPackage || currentPackage.found == false) {
console.error(`Current package id: ${currentPackage} not found`);
return data;
}

// check if the authorities match
if (attachPackage?._source.authority !== currentPackage._source.authority) {
console.error(
`Copy Attachment Package of id: ${copyAttachmentsFromId} does not have the same authority as ${currentPackage._id}.`,
);
return;
return data;
}

if (attachPackage) {
Expand All @@ -93,10 +71,46 @@ const copyAttachments = async ({
console.log("actual attachments:? ", JSON.stringify(attachments));
console.log("Current package: ", currentPackage);

// currentPackage.attachments = attachments;
// add the attachments to the last index of the currentPackage Change Log
const length = currentPackageChangelog.hits.hits.length;
currentPackageChangelog.hits.hits[length]._source.attachments = attachments;
console.log("Did I change it??", currentPackageChangelog.hits.hits[length]);

return currentPackageChangelog.hits.hits[length];
}
};

const sendSubmitMessage = async ({
packageId,
currentPackage,
copyAttachmentsFromId,
}: {
packageId: string;
currentPackage: ItemResult;
copyAttachmentsFromId?: string;
}) => {
const topicName = process.env.topicName as string;
if (!topicName) {
throw new Error("Topic name is not defined");
}
await produceMessage(
topicName,
packageId,
JSON.stringify({
...currentPackage._source,
copyAttachmentsFromId: copyAttachmentsFromId,
origin: "SEATool",
isAdminChange: true,
adminChangeType: "NOSO",
changeMade: `${packageId} added to OneMAC. Package not originally submitted in OneMAC. At this time, the attachments for this package are unavailable in this system. Contact your CPOC to verify the initial submission documents.`,
changeReason: `This is a Not Originally Submitted in OneMAC (NOSO) that users need to see in OneMAC.`,
}),
);

return currentPackage;
return response({
statusCode: 200,
body: { message: `${packageId} has been submitted.` },
});
};

export const handler = async (event: APIGatewayEvent) => {
Expand All @@ -108,10 +122,9 @@ export const handler = async (event: APIGatewayEvent) => {
}
try {
// add a property for new ID
const { packageId, action, copyAttachmentsFromId, changeIdTo } = submitNOSOAdminSchema.parse(
const { packageId, action, copyAttachmentsFromId } = submitNOSOAdminSchema.parse(
event.body === "string" ? JSON.parse(event.body) : event.body,
);
console.log("ID:", packageId);
let currentPackage: ItemResult | undefined = await getPackage(packageId);

Check failure on line 128 in lib/lambda/submit/submitNOSO.ts

View workflow job for this annotation

GitHub Actions / lint

'currentPackage' is never reassigned. Use 'const' instead

// currentpackage should have been entered in seaTool
Expand All @@ -122,22 +135,9 @@ export const handler = async (event: APIGatewayEvent) => {
});
}

// if there was a PackageId to copy attachments for perform that action
if (copyAttachmentsFromId) {
const tempCopy = await copyAttachments({ currentPackage, copyAttachmentsFromId });
// we don't want to rewrite over the package unless a valid currentPackage is returned.
if (tempCopy) currentPackage = tempCopy;
}

// if split spa & packageID to be changed
if (changeIdTo) {
currentPackage._id = changeIdTo;
currentPackage._source.id = changeIdTo;
console.log("CHANGE PACKAGE: ", currentPackage);
}

if (action === "NOSO" && currentPackage !== undefined) {
return await sendSubmitMessage({ packageId, currentPackage });
// copying over attachments is handled in sinkChangeLog
return await sendSubmitMessage({ packageId, currentPackage, copyAttachmentsFromId });
}

return response({
Expand Down
3 changes: 0 additions & 3 deletions react-app/src/features/package/admin-changes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ export const AdminChange: FC<opensearch.changelog.Document> = (props) => {
}
return ["Disable Formal RAI Response Withdraw", AC_WithdrawDisabled];
}
case "delete":
case "update-values":
case "update-id":
case "NOSO":
case "legacy-admin-change":
return [props.changeType || "Manual Update", AC_LegacyAdminChange];
Expand Down

0 comments on commit 54d7968

Please sign in to comment.