From bbef6ab72e1f71a0cbb81b8ec96ca91f74c525c7 Mon Sep 17 00:00:00 2001 From: Ben Stoltz Date: Tue, 14 May 2024 11:16:46 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20initiative=20fromEditor=20creates=20new?= =?UTF-8?q?=20initiatives=20before=20attempting=20=E2=80=A6=20(#1503)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/src/initiatives/HubInitiative.ts | 21 ++++++++--- .../test/initiatives/HubInitiative.test.ts | 35 +++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/common/src/initiatives/HubInitiative.ts b/packages/common/src/initiatives/HubInitiative.ts index 32b2e6ec267..dca25fff83e 100644 --- a/packages/common/src/initiatives/HubInitiative.ts +++ b/packages/common/src/initiatives/HubInitiative.ts @@ -326,9 +326,20 @@ export class HubInitiative const autoShareGroups = editor._groups || []; // 2. convert the editor values back to a initiative entity - const entity = await editorToInitiative(editor, this.context); + let entity = await editorToInitiative(editor, this.context); + + // 3. If the entity hasn't been created then we need to do that before we can + // create a featured image, if one has been provided. + if (!entity.id && featuredImage) { + // update this.entity so that the save method will work + this.entity = entity; + // save the entity to get an id / create it + await this.save(); + // update the local entity let so that the featured image call can pick up the id + entity = this.entity; + } - // 3. set the thumbnailCache to ensure that + // 4. set the thumbnailCache to ensure that // the thumbnail is updated on the next save if (thumbnail) { if (thumbnail.blob) { @@ -344,7 +355,7 @@ export class HubInitiative } } - // 4. upsert or remove the configured featured image + // 5. upsert or remove the configured featured image if (featuredImage) { let featuredImageUrl: string | null = null; if (featuredImage.blob) { @@ -376,11 +387,11 @@ export class HubInitiative }; } - // 5. save or create the entity + // 6. save or create the entity this.entity = entity; await this.save(); - // 6. share the entity with the configured groups + // 7. share the entity with the configured groups const isCreate = !editor.id; if (isCreate) { await this.setAccess(editor.access as SettableAccessLevel); diff --git a/packages/common/test/initiatives/HubInitiative.test.ts b/packages/common/test/initiatives/HubInitiative.test.ts index 1c8d5dcc979..b7bca885659 100644 --- a/packages/common/test/initiatives/HubInitiative.test.ts +++ b/packages/common/test/initiatives/HubInitiative.test.ts @@ -594,6 +594,41 @@ describe("HubInitiative Class:", () => { expect(createSpy).not.toHaveBeenCalled(); expect(upsertResourceSpy).toHaveBeenCalledTimes(1); }); + it("handles setting featured image on creation", async () => { + const chk = HubInitiative.fromJson( + { + name: "Test Entity", + }, + authdCtxMgr.context + ); + const editor = await chk.toEditor(); + editor.view = { + featuredImage: { + blob: "some blob", + filename: "some-featuredImage.png", + }, + }; + const upsertResourceSpy = spyOn( + upsertResourceModule, + "upsertResource" + ).and.returnValue( + Promise.resolve("https://blah.com/some-featuredImage.png") + ); + + editor.access = "org"; + + const accessSpy = spyOn( + HubItemEntity.prototype, + "setAccess" + ).and.returnValue(Promise.resolve()); + + await chk.fromEditor(editor); + expect(createSpy).toHaveBeenCalledTimes(1); + expect(updateSpy).toHaveBeenCalledTimes(1); + expect(upsertResourceSpy).toHaveBeenCalledTimes(1); + expect(accessSpy).toHaveBeenCalledTimes(1); + expect(accessSpy).toHaveBeenCalledWith("org"); + }); it("handles setting featured image and clearing prior image", async () => { const chk = HubInitiative.fromJson( {