PR comments

This commit is contained in:
mike12345567 2023-12-08 13:24:24 +00:00
parent f6d5db7d61
commit d4a4bb96f4
3 changed files with 12 additions and 15 deletions

View File

@ -79,7 +79,7 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
table,
})) as Row
// check if any attachments removed
await AttachmentCleanup.rowUpdate(table, row, oldRow)
await AttachmentCleanup.rowUpdate(table, { row, oldRow })
if (isUserTable) {
// the row has been updated, need to put it into the ctx

View File

@ -50,9 +50,6 @@ export class AttachmentCleanup {
return AttachmentCleanup.tableChange(table, rows, { deleting: true })
}
/**
* Cleanup attachments when updating a table.
*/
static async tableUpdate(
table: Table,
rows: Row[],
@ -61,9 +58,6 @@ export class AttachmentCleanup {
return AttachmentCleanup.tableChange(table, rows, opts)
}
/**
* Cleanup attachments when deleting rows.
*/
static async rowDelete(table: Table, rows: Row[]) {
return AttachmentCleanup.coreCleanup(() => {
let files: string[] = []
@ -81,10 +75,7 @@ export class AttachmentCleanup {
})
}
/**
* Remove attachments when updating a row, if new row doesn't have the attachments.
*/
static rowUpdate(table: Table, row: Row, oldRow: Row) {
static rowUpdate(table: Table, opts: { row: Row; oldRow: Row }) {
return AttachmentCleanup.coreCleanup(() => {
let files: string[] = []
for (let [key, schema] of Object.entries(table.schema)) {
@ -92,9 +83,12 @@ export class AttachmentCleanup {
continue
}
const oldKeys =
oldRow[key]?.map((attachment: RowAttachment) => attachment.key) || []
opts.oldRow[key]?.map(
(attachment: RowAttachment) => attachment.key
) || []
const newKeys =
row[key]?.map((attachment: RowAttachment) => attachment.key) || []
opts.row[key]?.map((attachment: RowAttachment) => attachment.key) ||
[]
files = files.concat(
oldKeys.filter((key: string) => newKeys.indexOf(key) === -1)
)

View File

@ -91,7 +91,10 @@ describe("attachment cleanup", () => {
it("should handle row updates", async () => {
const updatedRow = row()
delete updatedRow.attach
await AttachmentCleanup.rowUpdate(table(), updatedRow, row())
await AttachmentCleanup.rowUpdate(table(), {
row: updatedRow,
oldRow: row(),
})
expect(mockedDeleteFiles).toBeCalledWith(BUCKET, [FILE_NAME])
})
@ -101,7 +104,7 @@ describe("attachment cleanup", () => {
})
it("shouldn't cleanup attachments if row not updated", async () => {
await AttachmentCleanup.rowUpdate(table(), row(), row())
await AttachmentCleanup.rowUpdate(table(), { row: row(), oldRow: row() })
expect(mockedDeleteFiles).not.toBeCalled()
})
})