Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): functionality for updating ID, values, and deleting packages #923

Merged
merged 151 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 146 commits
Commits
Show all changes
151 commits
Select commit Hold shift + click to select a range
8a50578
wip
tiffanyvu Nov 25, 2024
cdfdb4e
check sinkmain
tiffanyvu Nov 25, 2024
26fd75b
remove deleted code
tiffanyvu Nov 25, 2024
46e3dc3
rename to handler
tiffanyvu Nov 25, 2024
5ffe3f3
add env config
tiffanyvu Nov 25, 2024
5d7df68
fix parse
tiffanyvu Nov 25, 2024
1183248
fix event body type
tiffanyvu Nov 25, 2024
5db0419
add origin
tiffanyvu Nov 25, 2024
b8814ee
add logs
tiffanyvu Nov 25, 2024
2bf5fb6
log
tiffanyvu Nov 25, 2024
77740a2
fix test and add log
tiffanyvu Nov 26, 2024
4791235
temp bypass record event
tiffanyvu Nov 26, 2024
444faf2
change order of sinkmain if
tiffanyvu Nov 26, 2024
bfc92eb
push into sinkmain
tiffanyvu Nov 26, 2024
b85b6ef
add id into doc
tiffanyvu Nov 26, 2024
0bfcbc4
log in sinkmain
tiffanyvu Nov 26, 2024
89520bf
format deleted package
tiffanyvu Nov 26, 2024
21f1968
add sink change log
tiffanyvu Nov 26, 2024
a981b4a
change order of sinklog changelog
tiffanyvu Nov 26, 2024
839f218
fix schema
tiffanyvu Nov 26, 2024
4f29673
add logs changelog
tiffanyvu Nov 26, 2024
2830a45
try adding delete property
tiffanyvu Nov 26, 2024
5a7a65a
fix property
tiffanyvu Nov 26, 2024
96bf0d8
log query
tiffanyvu Nov 26, 2024
b4ff2cd
change term
tiffanyvu Nov 26, 2024
8b1e39e
letter s
tiffanyvu Nov 26, 2024
db5e947
must not
tiffanyvu Nov 26, 2024
b350788
wip update package fields
tiffanyvu Nov 26, 2024
c4d5e23
add log
tiffanyvu Nov 26, 2024
bfee3a8
omg
tiffanyvu Nov 27, 2024
2c8a6ff
try fix test and changelog update fields
tiffanyvu Nov 27, 2024
f20f10a
transform updated values?
tiffanyvu Nov 27, 2024
2936adb
typo
tiffanyvu Nov 27, 2024
bab9ab9
again
tiffanyvu Nov 27, 2024
07f656f
try
tiffanyvu Nov 27, 2024
a890751
remove origin
tiffanyvu Nov 27, 2024
231bee9
add timestamp?
tiffanyvu Nov 27, 2024
dd76406
fix changelog not showing
tiffanyvu Nov 27, 2024
428f1e0
edit changelog text
tiffanyvu Nov 27, 2024
26a80a9
meg
tiffanyvu Nov 27, 2024
cd8e0d2
changelog admin changes updates, language TBD
tiffanyvu Nov 27, 2024
b4afe70
edit schema parsing for sinkmain
tiffanyvu Nov 27, 2024
333a867
change schema parsing in changelog
tiffanyvu Nov 27, 2024
c877258
rm commented out code
tiffanyvu Nov 28, 2024
54681e2
fix eslint
tiffanyvu Nov 28, 2024
97fe983
clean up
tiffanyvu Dec 2, 2024
0c703fa
remove more logs
tiffanyvu Dec 3, 2024
78cf0d7
readd comment
tiffanyvu Dec 3, 2024
856adb3
cleanup zod schema logic
tiffanyvu Dec 3, 2024
a63755a
change event name
tiffanyvu Dec 3, 2024
4232711
test adminchange for delete
tiffanyvu Dec 3, 2024
6469a36
attachments is undefined for soft deleted packages, add optional
tiffanyvu Dec 3, 2024
5d3183f
Merge branch 'main' into lambda-update-package
tiffanyvu Dec 4, 2024
56a0ab0
wip
tiffanyvu Dec 5, 2024
634e51c
revert safeparse
tiffanyvu Dec 9, 2024
6aa7b0a
remove empty test
tiffanyvu Dec 9, 2024
c45832d
set default attachments value
tiffanyvu Dec 9, 2024
a511c72
move default attachments default value
tiffanyvu Dec 9, 2024
d2de296
delete test, will be handled by sinkmain tests
tiffanyvu Dec 9, 2024
95f8d5b
edit todo msg
tiffanyvu Dec 9, 2024
58e7321
rm unused code
tiffanyvu Dec 9, 2024
601189e
Merge branch 'main' into lambda-update-package
tiffanyvu Dec 9, 2024
d30bba8
Merge branch 'main' into lambda-update-id
tiffanyvu Dec 9, 2024
0d31401
remove origin property
tiffanyvu Dec 9, 2024
abc9719
Merge branch 'main' into lambda-update-id
tiffanyvu Dec 10, 2024
60c31d4
Merge branch 'main' into lambda-update-package
tiffanyvu Dec 10, 2024
a8a6dbc
test
tiffanyvu Dec 10, 2024
e786b18
check for existing package id
tiffanyvu Dec 10, 2024
35b52ca
add update id schema
tiffanyvu Dec 10, 2024
66f6cd8
add admin change type
tiffanyvu Dec 10, 2024
e979456
typo
tiffanyvu Dec 10, 2024
d0c16ed
again
tiffanyvu Dec 10, 2024
8442250
add log
tiffanyvu Dec 10, 2024
f43b796
edit log
tiffanyvu Dec 10, 2024
b7a04cb
edit changelog
tiffanyvu Dec 10, 2024
b2f7ec0
exclude _index
tiffanyvu Dec 10, 2024
e6a38df
fix destructuring
tiffanyvu Dec 10, 2024
684c055
source?
tiffanyvu Dec 10, 2024
81dafd2
test log
tiffanyvu Dec 11, 2024
01d0e5c
hide changelog
tiffanyvu Dec 11, 2024
37e424b
ew test
tiffanyvu Dec 11, 2024
46cb421
add changemade and types to Document type
tiffanyvu Dec 11, 2024
38633c1
add zod to event body
tiffanyvu Dec 11, 2024
266f791
update response status codes
tiffanyvu Dec 11, 2024
f807d7e
Merge branch 'main' into lambda-update-package
tiffanyvu Dec 11, 2024
a59e302
Merge branch 'main' into lambda-update-id
tiffanyvu Dec 11, 2024
ffaf219
Merge branch 'lambda-update-package' into lambda-update-package-funct…
tiffanyvu Dec 11, 2024
c3fb5f5
merge and rm console log
tiffanyvu Dec 11, 2024
b391d25
rm comment
tiffanyvu Dec 11, 2024
493e31e
wip id validation
tiffanyvu Dec 11, 2024
b742679
Merge branch 'main' into update-package-func
asharonbaltazar Dec 12, 2024
5565b2a
test logs
tiffanyvu Dec 12, 2024
aacc361
Merge branch 'update-package-func' of https://github.com/Enterprise-C…
tiffanyvu Dec 12, 2024
3646259
again
tiffanyvu Dec 12, 2024
978ca6d
add logs
tiffanyvu Dec 12, 2024
556aac6
logs
tiffanyvu Dec 12, 2024
73c359c
fix
tiffanyvu Dec 12, 2024
3b6686b
Merge branch 'main' into update-package-func
asharonbaltazar Dec 12, 2024
d45535b
test format
tiffanyvu Dec 12, 2024
f0da974
log
tiffanyvu Dec 12, 2024
902588d
test moving responses into funcs
tiffanyvu Dec 12, 2024
4b7dc81
test
tiffanyvu Dec 12, 2024
0d42864
fix punctuation
tiffanyvu Dec 13, 2024
af196d6
wip address comments
tiffanyvu Dec 13, 2024
fa838cd
rm old code
tiffanyvu Dec 13, 2024
aad435d
add comment
tiffanyvu Dec 13, 2024
d1febcc
Merge branch 'main' into update-package-func
tiffanyvu Dec 13, 2024
ecceed5
rm eslint ignore
tiffanyvu Dec 13, 2024
7d003b3
move topicname logic
tiffanyvu Dec 13, 2024
2f68a24
test package event type in changelog
tiffanyvu Dec 13, 2024
15f00c0
log
tiffanyvu Dec 13, 2024
654aa38
logs
tiffanyvu Dec 13, 2024
64177f3
fix
tiffanyvu Dec 13, 2024
3479e33
move event
tiffanyvu Dec 13, 2024
f3df9b1
move logic
tiffanyvu Dec 13, 2024
78a4d61
logs
tiffanyvu Dec 13, 2024
d86b23f
typo
tiffanyvu Dec 13, 2024
dbcd80f
what
tiffanyvu Dec 13, 2024
b172846
log
tiffanyvu Dec 13, 2024
24e86fd
add mako origin to get to transforms
tiffanyvu Dec 13, 2024
5d968b4
umm touching opensearch
tiffanyvu Dec 13, 2024
a81684e
typo
tiffanyvu Dec 14, 2024
f1bdb10
logs
tiffanyvu Dec 16, 2024
f9e5858
revert
tiffanyvu Dec 16, 2024
7c897d1
try
tiffanyvu Dec 16, 2024
be77182
revert
tiffanyvu Dec 16, 2024
482422c
revert changelog
tiffanyvu Dec 16, 2024
2671c39
hi
tiffanyvu Dec 16, 2024
6aed3b0
logs
tiffanyvu Dec 16, 2024
176e985
add old id to changelog
tiffanyvu Dec 16, 2024
1d25971
offset stuff
tiffanyvu Dec 16, 2024
140485b
logs
tiffanyvu Dec 16, 2024
7b967ed
logs
tiffanyvu Dec 16, 2024
a1e0ea4
duplicate offset
tiffanyvu Dec 16, 2024
31aafc0
fix
tiffanyvu Dec 17, 2024
0a5494b
log hits
tiffanyvu Dec 17, 2024
85217cf
logs
tiffanyvu Dec 17, 2024
9879466
test
tiffanyvu Dec 17, 2024
0a83a4b
fix
tiffanyvu Dec 17, 2024
090e570
fix
tiffanyvu Dec 17, 2024
257ed25
idToBeUpdated
tiffanyvu Dec 17, 2024
5df41a9
add onemac
tiffanyvu Dec 17, 2024
d06cf1d
fix
tiffanyvu Dec 17, 2024
6e1e5e2
clean
tiffanyvu Dec 17, 2024
c0c2906
move around
tiffanyvu Dec 17, 2024
a3575f1
clean
tiffanyvu Dec 17, 2024
622aad9
remove casting
tiffanyvu Dec 18, 2024
2d33626
casting throws type error
tiffanyvu Dec 18, 2024
099d878
sinkmain conflicts
tiffanyvu Dec 18, 2024
b9bd169
fix import and add test
tiffanyvu Dec 18, 2024
ce4ac62
Merge branch 'main' into update-package-func
tiffanyvu Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
8 changes: 4 additions & 4 deletions lib/lambda/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { describe, it, expect, vi, beforeEach, Mock } from "vitest";
import { APIGatewayEvent } from "aws-lambda";
import { handler } from "./search";
import { response } from "libs/handler-lib";
Expand Down Expand Up @@ -52,8 +52,8 @@ describe("getSearchData Handler", () => {
},
};

(os.search as vi.Mock).mockResolvedValueOnce(mockResults);
(getAppkChildren as vi.Mock).mockResolvedValueOnce({
(os.search as Mock).mockResolvedValueOnce(mockResults);
(getAppkChildren as Mock).mockResolvedValueOnce({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap these calls with vi.mocked to get the same benefit without casting

hits: {
hits: [{ _id: "2", _source: { child: "child-data" } }],
},
Expand All @@ -62,7 +62,7 @@ describe("getSearchData Handler", () => {
const event = {
pathParameters: { index: "main" },
body: JSON.stringify({ query: { match_all: {} } }),
} as APIGatewayEvent;
} as unknown as APIGatewayEvent;

await handler(event);

Expand Down
2 changes: 2 additions & 0 deletions lib/lambda/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const getSearchData = async (event: APIGatewayEvent) => {
query.query = query?.query || {};
query.query.bool = query.query?.bool || {};
query.query.bool.must = query.query.bool?.must || [];
query.query.bool.must_not = query.query.bool?.must_not || [];
query.query.bool.must_not.push({ term: { deleted: true } });

const stateFilter = await getStateFilter(event);
if (stateFilter) {
Expand Down
55 changes: 48 additions & 7 deletions lib/lambda/sinkChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import {
logError,
} from "../libs/sink-lib";
import { Index } from "shared-types/opensearch";
import {
transformDeleteSchema,
transformUpdateValuesSchema,
transformedUpdateIdSchema,
} from "./update/adminChangeSchemas";
import { getPackageChangelog } from "lib/libs/api/package";
const osDomain = process.env.osDomain;
if (!osDomain) {
throw new Error("Missing required environment variable(s)");
Expand Down Expand Up @@ -61,11 +67,11 @@ const processAndIndex = async ({
transforms: any;
topicPartition: string;
}) => {
const docs: Array<(typeof transforms)[keyof typeof transforms]["Schema"]> =
[];
const docs: Array<(typeof transforms)[keyof typeof transforms]["Schema"]> = [];
for (const kafkaRecord of kafkaRecords) {
console.log(JSON.stringify(kafkaRecord, null, 2));
const { value, offset } = kafkaRecord;

try {
// If a legacy tombstone, continue
if (!value) {
Expand All @@ -75,19 +81,54 @@ const processAndIndex = async ({
// Parse the kafka record's value
const record = JSON.parse(decodeBase64WithUtf8(value));

// query all changelog entries for this ID and create copies of all entries with new ID
if (record.isAdminChange) {
const schema = transformDeleteSchema(offset).or(
transformUpdateValuesSchema(offset).or(transformedUpdateIdSchema),
);

const result = schema.safeParse(record);

if (result.success) {
if (result.data.adminChangeType === "update-id") {
docs.forEach((log) => {
const recordOffset = log.id.split("-").at(-1);

docs.push({
...log,
id: `${result.data.id}-${recordOffset}`,
packageId: result.data.id,
});
});
const packageChangelogs = await getPackageChangelog(result.data.idToBeUpdated);

packageChangelogs.hits.hits.forEach((log) => {
const recordOffset = log._id.split("-").at(-1);
docs.push({
...log._source,
id: `${result.data.id}-${recordOffset}`,
packageId: result.data.id,
});
});
} else {
docs.push(result.data);
}
} else {
console.log(
`Skipping package with invalid format for type "${record.adminChangeType}"`,
result.error.message,
);
}
}
// If we're not a mako event, continue
// TODO: handle legacy. for now, just continue
if (!record.event || record?.origin !== "mako") {
continue;
}

// If the event is a supported event, transform and push to docs array for indexing
console.log("event below");
console.log(record.event);

if (record.event in transforms) {
const transformForEvent =
transforms[record.event as keyof typeof transforms];
const transformForEvent = transforms[record.event as keyof typeof transforms];

const result = transformForEvent.transform(offset).safeParse(record);

Expand Down
21 changes: 21 additions & 0 deletions lib/lambda/sinkMain.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file changed significantly. Lemme know if you wanna pair up

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { ErrorType, bulkUpdateDataWrapper, getTopic, logError } from "../libs/si
import { Index } from "shared-types/opensearch";
import { decodeBase64WithUtf8 } from "shared-utils";
import * as os from "./../libs/opensearch-lib";
import {
deleteAdminChangeSchema,
updateValuesAdminChangeSchema,
updateIdAdminChangeSchema,
} from "./update/adminChangeSchemas";

const osDomain = process.env.osDomain;
const indexNamespace = process.env.indexNamespace;
Expand Down Expand Up @@ -81,6 +86,22 @@ const processAndIndex = async ({

// If we're not a mako event, continue
// TODO: handle legacy. for now, just continue

const schema = deleteAdminChangeSchema
.or(updateValuesAdminChangeSchema)
.or(updateIdAdminChangeSchema);

if (record.isAdminChange) {
const result = schema.safeParse(record);
if (result.success) {
docs.push(record);
} else {
console.log(
`Skipping package with invalid format for type "${record.adminChangeType}"`,
result.error.message,
);
}
}
if (!record.event || record?.origin !== "mako") {
continue;
}
Expand Down
50 changes: 50 additions & 0 deletions lib/lambda/update/adminChangeSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { z } from "zod";

export const deleteAdminChangeSchema = z
.object({
id: z.string(),
deleted: z.boolean(),
adminChangeType: z.literal("delete"),
})
.and(z.record(z.string(), z.any()));

export const updateValuesAdminChangeSchema = z
.object({
id: z.string(),
adminChangeType: z.literal("update-values"),
})
.and(z.record(z.string(), z.any()));

export const updateIdAdminChangeSchema = z
.object({
id: z.string(),
adminChangeType: z.literal("update-id"),
idToBeUpdated: z.string(),
})
.and(z.record(z.string(), z.any()));

export const transformDeleteSchema = (offset: number) =>
deleteAdminChangeSchema.transform((data) => ({
...data,
event: "delete",
packageId: data.id,
id: `${data.id}-${offset}`,
timestamp: Date.now(),
}));

export const transformUpdateValuesSchema = (offset: number) =>
updateValuesAdminChangeSchema.transform((data) => ({
...data,
event: "update-values",
packageId: data.id,
id: `${data.id}-${offset}`,
timestamp: Date.now(),
}));

export const transformedUpdateIdSchema = updateIdAdminChangeSchema.transform((data) => ({
...data,
event: "update-id",
packageId: data.id,
id: `${data.id}`,
timestamp: Date.now(),
}));
25 changes: 25 additions & 0 deletions lib/lambda/update/getPackageType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { response } from "lib/libs/handler-lib";
import { events } from "lib/packages/shared-types";
import { getPackageChangelog } from "lib/libs/api/package";

export const getPackageType = async (packageId: string) => {
// use event of current package to determine how ID should be formatted
try {
const packageChangelog = await getPackageChangelog(packageId);
if (!packageChangelog.hits.hits.length) {
throw new Error("The type of package could not be determined.");
}

const packageWithSubmissionType = packageChangelog.hits.hits.find((pkg) => {
return pkg._source.event in events;
});
const packageEvent = packageWithSubmissionType?._source.event;
tiffanyvu marked this conversation as resolved.
Show resolved Hide resolved

return packageEvent;
} catch (error) {
return response({
statusCode: 500,
body: { message: error },
});
}
};
Loading