Commit 2a27215d authored by nanahira's avatar nanahira

fix dupes in batch create

parent dd8b8b71
...@@ -79,6 +79,8 @@ export class CrudBase<T extends ValidCrudEntity<T>> { ...@@ -79,6 +79,8 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
let skipped: { result: string; entry: T }[] = []; let skipped: { result: string; entry: T }[] = [];
const repo = mdb.getRepository(this.entityClass); const repo = mdb.getRepository(this.entityClass);
let entsToSave = ents;
if (entsWithId.length) { if (entsWithId.length) {
const existingEnts = await repo.find({ const existingEnts = await repo.find({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
...@@ -111,7 +113,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> { ...@@ -111,7 +113,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
entry: ent, entry: ent,
})); }));
const skippedEntsSet = new Set(skippedEnts); const skippedEntsSet = new Set(skippedEnts);
ents = ents.filter((ent) => !skippedEntsSet.has(ent)); entsToSave = ents.filter((ent) => !skippedEntsSet.has(ent));
} }
if (existingEntsWithDeleteTime.length) { if (existingEntsWithDeleteTime.length) {
await repo.delete( await repo.delete(
...@@ -124,14 +126,16 @@ export class CrudBase<T extends ValidCrudEntity<T>> { ...@@ -124,14 +126,16 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
await beforeCreate(repo); await beforeCreate(repo);
} }
try { try {
const results = await repo.save(ents as DeepPartial<T>[]); const results = await repo.save(entsToSave as DeepPartial<T>[]);
return { return {
results, results,
skipped, skipped,
}; };
} catch (e) { } catch (e) {
this.log.error( this.log.error(
`Failed to create entity ${JSON.stringify(ents)}: ${e.toString()}`, `Failed to create entity ${JSON.stringify(
entsToSave,
)}: ${e.toString()}`,
); );
throw new BlankReturnMessageDto(500, 'Internal error').toException(); throw new BlankReturnMessageDto(500, 'Internal error').toException();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment