fix(server): tag upsert (#12141)

This commit is contained in:
Jason Rasmussen
2024-08-30 12:44:24 -04:00
committed by GitHub
parent b9e5e40ced
commit 9b1a985d29
14 changed files with 163 additions and 41 deletions

View File

@@ -22,6 +22,48 @@ export class TagRepository implements ITagRepository {
return this.repository.findOne({ where: { userId, value } });
}
async upsertValue({
userId,
value,
parent,
}: {
userId: string;
value: string;
parent?: TagEntity;
}): Promise<TagEntity> {
return this.dataSource.transaction(async (manager) => {
// upsert tag
const { identifiers } = await manager.upsert(
TagEntity,
{ userId, value, parentId: parent?.id },
{ conflictPaths: { userId: true, value: true } },
);
const id = identifiers[0]?.id;
if (!id) {
throw new Error('Failed to upsert tag');
}
// update closure table
await manager.query(
`INSERT INTO tags_closure (id_ancestor, id_descendant)
VALUES ($1, $1)
ON CONFLICT DO NOTHING;`,
[id],
);
if (parent) {
await manager.query(
`INSERT INTO tags_closure (id_ancestor, id_descendant)
SELECT id_ancestor, '${id}' as id_descendant FROM tags_closure WHERE id_descendant = $1
ON CONFLICT DO NOTHING`,
[parent.id],
);
}
return manager.findOneOrFail(TagEntity, { where: { id } });
});
}
async getAll(userId: string): Promise<TagEntity[]> {
const tags = await this.repository.find({
where: { userId },