diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index f9e05c5bd8..f7a1c3aa6f 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -237,6 +237,11 @@ describe.each([ name: "attachment", constraints: { type: "array", presence: false }, } + const signature: FieldSchema = { + type: FieldType.SIGNATURE, + name: "signature", + constraints: { type: "array", presence: false }, + } const bool: FieldSchema = { type: FieldType.BOOLEAN, name: "boolean", @@ -301,6 +306,10 @@ describe.each([ attachmentUndefined: attachment, attachmentEmpty: attachment, attachmentEmptyArrayStr: attachment, + signatureNull: signature, + signatureUndefined: signature, + signatureEmpty: signature, + signatureEmptyArrayStr: signature, arrayFieldEmptyArrayStr: arrayField, arrayFieldArrayStrKnown: arrayField, arrayFieldNull: arrayField, @@ -340,6 +349,10 @@ describe.each([ attachmentUndefined: undefined, attachmentEmpty: "", attachmentEmptyArrayStr: "[]", + signatureNull: null, + signatureUndefined: undefined, + signatureEmpty: "", + signatureEmptyArrayStr: "[]", arrayFieldEmptyArrayStr: "[]", arrayFieldUndefined: undefined, arrayFieldNull: null, @@ -372,6 +385,10 @@ describe.each([ expect(row.attachmentUndefined).toBe(undefined) expect(row.attachmentEmpty).toEqual([]) expect(row.attachmentEmptyArrayStr).toEqual([]) + expect(row.signatureNull).toEqual([]) + expect(row.signatureUndefined).toBe(undefined) + expect(row.signatureEmpty).toEqual([]) + expect(row.signatureEmptyArrayStr).toEqual([]) expect(row.arrayFieldEmptyArrayStr).toEqual([]) expect(row.arrayFieldNull).toEqual([]) expect(row.arrayFieldUndefined).toEqual(undefined) @@ -783,24 +800,21 @@ describe.each([ }) isInternal && - describe("attachments", () => { - it("should allow enriching attachment rows", async () => { + describe("attachments and signatures", () => { + const coreAttachmentEnrichment = async ( + schema: any, + field: string, + attachmentId: string + ) => { const table = await config.api.table.save( defaultTable({ - schema: { - attachment: { - type: FieldType.ATTACHMENT, - name: "attachment", - constraints: { type: "array", presence: false }, - }, - }, + schema, }) ) - const attachmentId = `${uuid.v4()}.csv` const row = await config.api.row.save(table._id!, { name: "test", description: "test", - attachment: [ + [field]: [ { key: `${config.getAppId()}/attachments/${attachmentId}`, }, @@ -810,11 +824,39 @@ describe.each([ await config.withEnv({ SELF_HOSTED: "true" }, async () => { return context.doInAppContext(config.getAppId(), async () => { const enriched = await outputProcessing(table, [row]) - expect((enriched as Row[])[0].attachment[0].url).toBe( + expect((enriched as Row[])[0]?.[field][0].url).toBe( `/files/signed/prod-budi-app-assets/${config.getProdAppId()}/attachments/${attachmentId}` ) }) }) + } + + it("should allow enriching attachment rows", async () => { + coreAttachmentEnrichment( + { + attachment: { + type: FieldType.ATTACHMENT, + name: "attachment", + constraints: { type: "array", presence: false }, + }, + }, + "attachment", + `${uuid.v4()}.csv` + ) + }) + + it("should allow enriching signature rows", async () => { + coreAttachmentEnrichment( + { + signature: { + type: FieldType.SIGNATURE, + name: "signature", + constraints: { type: "array", presence: false }, + }, + }, + "signature", + `${uuid.v4()}.png` + ) }) }) diff --git a/packages/server/src/sdk/app/rows/attachments.ts b/packages/server/src/sdk/app/rows/attachments.ts index 2ab9e83c47..44d2764798 100644 --- a/packages/server/src/sdk/app/rows/attachments.ts +++ b/packages/server/src/sdk/app/rows/attachments.ts @@ -30,7 +30,10 @@ export async function getRowsWithAttachments(appId: string, table: Table) { const db = dbCore.getDB(appId) const attachmentCols: string[] = [] for (let [key, column] of Object.entries(table.schema)) { - if (column.type === FieldType.ATTACHMENT) { + if ( + column.type === FieldType.ATTACHMENT || + column.type === FieldType.SIGNATURE + ) { attachmentCols.push(key) } } diff --git a/packages/server/src/sdk/tests/attachments.spec.ts b/packages/server/src/sdk/tests/attachments.spec.ts index c1736e6f8e..feaacce9a2 100644 --- a/packages/server/src/sdk/tests/attachments.spec.ts +++ b/packages/server/src/sdk/tests/attachments.spec.ts @@ -23,27 +23,18 @@ describe("should be able to re-write attachment URLs", () => { await config.init() }) - it("should update URLs on a number of rows over the limit", async () => { + const coreBehaviour = async (tblSchema: any, field: string) => { const table = await config.api.table.save({ name: "photos", type: "table", sourceId: INTERNAL_TABLE_SOURCE_ID, sourceType: TableSourceType.INTERNAL, - schema: { - photo: { - type: FieldType.ATTACHMENT, - name: "photo", - }, - otherCol: { - type: FieldType.STRING, - name: "otherCol", - }, - }, + schema: tblSchema, }) for (let i = 0; i < FIND_LIMIT * 4; i++) { await config.api.row.save(table._id!, { - photo: [attachment], + [field]: [attachment], otherCol: "string", }) } @@ -56,8 +47,39 @@ describe("should be able to re-write attachment URLs", () => { ) for (const row of rows) { expect(row.otherCol).toBe("string") - expect(row.photo[0].url).toBe("") - expect(row.photo[0].key).toBe(`${db.name}/attachments/a.png`) + expect(row[field][0].url).toBe("") + expect(row[field][0].key).toBe(`${db.name}/attachments/a.png`) } + } + + it("Attachment field, should update URLs on a number of rows over the limit", async () => { + await coreBehaviour( + { + photo: { + type: FieldType.ATTACHMENT, + name: "photo", + }, + otherCol: { + type: FieldType.STRING, + name: "otherCol", + }, + }, + "photo" + ) + }) + it("Signature field, should update URLs on a number of rows over the limit", async () => { + await coreBehaviour( + { + signature: { + type: FieldType.SIGNATURE, + name: "signature", + }, + otherCol: { + type: FieldType.STRING, + name: "otherCol", + }, + }, + "signature" + ) }) }) diff --git a/packages/server/src/utilities/rowProcessor/attachments.ts b/packages/server/src/utilities/rowProcessor/attachments.ts index 2b1c53cd4b..5690a32655 100644 --- a/packages/server/src/utilities/rowProcessor/attachments.ts +++ b/packages/server/src/utilities/rowProcessor/attachments.ts @@ -95,7 +95,10 @@ export class AttachmentCleanup { return AttachmentCleanup.coreCleanup(() => { let files: string[] = [] for (let [key, schema] of Object.entries(table.schema)) { - if (schema.type !== FieldType.ATTACHMENT) { + if ( + schema.type !== FieldType.ATTACHMENT && + schema.type !== FieldType.SIGNATURE + ) { continue } const oldKeys =