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

View File

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