templates aus listen erstellen

This commit is contained in:
Bastian Wagner
2026-06-15 10:38:35 +02:00
parent c2d2157de8
commit 8caf207c7b
8 changed files with 243 additions and 4 deletions

View File

@@ -10,6 +10,8 @@ import { InjectRepository } from '@nestjs/typeorm';
import { randomUUID } from 'crypto';
import { IsNull, Repository } from 'typeorm';
import { AuditLogService } from '../audit/audit-log.service';
import { ListTemplateEntity } from '../list-templates/list-template.entity';
import { ListTemplateItemEntity } from '../list-templates/list-template-item.entity';
import {
ListTemplate,
ListTemplateKind,
@@ -69,6 +71,12 @@ export class ListsService {
private readonly auditLogService?: AuditLogService,
@Optional()
private readonly listRealtimeService?: ListRealtimeService,
@Optional()
@InjectRepository(ListTemplateEntity)
private readonly templatesRepository?: Repository<ListTemplateEntity>,
@Optional()
@InjectRepository(ListTemplateItemEntity)
private readonly templateItemsRepository?: Repository<ListTemplateItemEntity>,
) {}
async createList(ownerId: string, createDto: CreateListDto): Promise<UserList> {
@@ -501,6 +509,56 @@ export class ListsService {
return { suggestions };
}
async createTemplateFromList(
ownerId: string,
listId: string,
): Promise<ListTemplate> {
if (!this.templatesRepository || !this.templateItemsRepository) {
throw new ServiceUnavailableException(
'List template storage is not configured.',
);
}
const list = await this.findAccessibleList(ownerId, listId);
const template = this.templatesRepository.create({
id: randomUUID(),
ownerId,
name: list.name,
description: list.description,
kind: list.kind,
deletedAt: null,
items: list.items
.sort((left, right) => left.position - right.position)
.map((item, index) =>
this.templateItemsRepository!.create({
id: randomUUID(),
title: item.title,
notes: item.notes,
quantity: item.quantity,
required: item.required,
position: index,
}),
),
});
const savedTemplate = await this.templatesRepository.save(template);
await this.auditLogService?.record({
actorUserId: ownerId,
action: 'template.created_from_list',
entityType: 'template',
entityId: savedTemplate.id,
metadata: {
sourceListId: list.id,
sourceListName: list.name,
name: savedTemplate.name,
kind: savedTemplate.kind,
itemCount: savedTemplate.items?.length ?? 0,
},
});
return this.toListTemplate(savedTemplate);
}
private async findAccessibleList(
ownerId: string,
listId: string,
@@ -760,6 +818,30 @@ export class ListsService {
};
}
private toListTemplate(template: ListTemplateEntity): ListTemplate {
return {
id: template.id,
ownerId: template.ownerId,
name: template.name,
description: template.description ?? undefined,
kind: template.kind,
items: (template.items ?? [])
.sort((left, right) => left.position - right.position)
.map((item) => ({
id: item.id,
title: item.title,
notes: item.notes ?? undefined,
quantity: item.quantity ?? undefined,
required: item.required,
position: item.position,
createdAt: this.toIsoString(item.createdAt),
updatedAt: this.toIsoString(item.updatedAt),
})),
createdAt: this.toIsoString(template.createdAt),
updatedAt: this.toIsoString(template.updatedAt),
};
}
private toIsoString(value?: Date): string {
return (value ?? new Date()).toISOString();
}