Skip to content

Commit 716205a

Browse files
committed
fix: fix error when updating a software without providing similar software
1 parent 5dd71cc commit 716205a

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

api/src/core/adapters/dbApi/kysely/createPgSoftwareRepository.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,21 @@ export const createPgSoftwareRepository = (db: Kysely<Database>): SoftwareReposi
184184
}));
185185
});
186186

187-
await db
188-
.insertInto("software_external_datas")
189-
.values(
190-
dataToInsert.map(({ similarExternalId, sourceSlug }) => ({
191-
externalId: similarExternalId,
192-
sourceSlug,
193-
label: JSON.stringify(""),
194-
description: JSON.stringify(""),
195-
developers: JSON.stringify([])
196-
}))
197-
)
198-
.onConflict(oc => oc.doNothing())
199-
.execute();
187+
if (dataToInsert.length > 0) {
188+
await db
189+
.insertInto("software_external_datas")
190+
.values(
191+
dataToInsert.map(({ similarExternalId, sourceSlug }) => ({
192+
externalId: similarExternalId,
193+
sourceSlug,
194+
label: JSON.stringify(""),
195+
description: JSON.stringify(""),
196+
developers: JSON.stringify([])
197+
}))
198+
)
199+
.onConflict(oc => oc.doNothing())
200+
.execute();
201+
}
200202

201203
await db.transaction().execute(async trx => {
202204
await trx
@@ -208,11 +210,13 @@ export const createPgSoftwareRepository = (db: Kysely<Database>): SoftwareReposi
208210
)
209211
.execute();
210212

211-
await trx
212-
.insertInto("softwares__similar_software_external_datas")
213-
.values(dataToInsert)
214-
.onConflict(oc => oc.columns(["softwareId", "sourceSlug", "similarExternalId"]).doNothing())
215-
.execute();
213+
if (dataToInsert.length > 0) {
214+
await trx
215+
.insertInto("softwares__similar_software_external_datas")
216+
.values(dataToInsert)
217+
.onConflict(oc => oc.columns(["softwareId", "sourceSlug", "similarExternalId"]).doNothing())
218+
.execute();
219+
}
216220
});
217221
},
218222
getSimilarSoftwareExternalDataPks: async ({ softwareId }) => {

api/src/core/usecases/updateSoftware.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,21 @@ describe("Create software, than updates it adding a similar software", () => {
142142
{ sourceSlug: testSource.slug, externalId: "Q111590996", softwareId: undefined },
143143
{ sourceSlug: testSource.slug, externalId: "Q56062435", softwareId: undefined }
144144
]);
145+
146+
// than update the software again, removing all similar software:
147+
const formDataWithNoSimilarSoftware: SoftwareFormData = {
148+
...craSoftwareFormData,
149+
similarSoftwareExternalDataIds: []
150+
};
151+
await updateSoftware({
152+
formData: formDataWithNoSimilarSoftware,
153+
softwareId: craSoftwareId,
154+
userId
155+
});
156+
157+
const finalSimilarSofts = await dbApi.software.getSimilarSoftwareExternalDataPks({
158+
softwareId: craSoftwareId
159+
});
160+
expectToMatchObject(finalSimilarSofts, []);
145161
});
146162
});

0 commit comments

Comments
 (0)