|
|
@@ -41,6 +41,34 @@ export class ChannelService {
|
|
|
return typeof value === 'string' ? value.trim() : value;
|
|
|
}
|
|
|
|
|
|
+ private async resolveCategoriesByIds(ids: string[]) {
|
|
|
+ if (!ids.length) return [];
|
|
|
+
|
|
|
+ const categories = await this.mongoPrismaService.category.findMany({
|
|
|
+ where: { id: { in: ids } },
|
|
|
+ select: { id: true, name: true },
|
|
|
+ });
|
|
|
+
|
|
|
+ return categories.map((c) => ({
|
|
|
+ id: c.id,
|
|
|
+ name: c.name,
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ private async resolveTagsByIds(ids: string[]) {
|
|
|
+ if (!ids.length) return [];
|
|
|
+
|
|
|
+ const tags = await this.mongoPrismaService.tag.findMany({
|
|
|
+ where: { id: { in: ids } },
|
|
|
+ select: { id: true, name: true },
|
|
|
+ });
|
|
|
+
|
|
|
+ return tags.map((t) => ({
|
|
|
+ id: t.id,
|
|
|
+ name: t.name,
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
async create(dto: CreateChannelDto) {
|
|
|
// Check for duplicate channel name
|
|
|
const existingChannel = await this.mongoPrismaService.channel.findFirst({
|
|
|
@@ -67,6 +95,14 @@ export class ChannelService {
|
|
|
const isDefault = (await this.mongoPrismaService.channel.count()) === 0;
|
|
|
|
|
|
const now = this.now();
|
|
|
+ const categoryIds = dto.categories?.map((c) => c.id) ?? [];
|
|
|
+ const tagIds = dto.tags?.map((t) => t.id) ?? [];
|
|
|
+
|
|
|
+ const [categories, tags] = await Promise.all([
|
|
|
+ this.resolveCategoriesByIds(categoryIds),
|
|
|
+ this.resolveTagsByIds(tagIds),
|
|
|
+ ]);
|
|
|
+ const tagNames = tags.map((t) => t.name.trim());
|
|
|
|
|
|
const channel = await this.mongoPrismaService.channel.create({
|
|
|
data: {
|
|
|
@@ -79,9 +115,9 @@ export class ChannelService {
|
|
|
clientNotice: this.trimOptional(dto.clientNotice) ?? null,
|
|
|
remark: this.trimOptional(dto.remark) ?? null,
|
|
|
isDefault,
|
|
|
- categories: (dto.categories as any) || null,
|
|
|
- tags: (dto.tags as any) || null,
|
|
|
- tagNames: dto.tagNames || [],
|
|
|
+ categories: categories.length ? categories : null,
|
|
|
+ tags: tags.length ? tags : null,
|
|
|
+ tagNames: tagNames,
|
|
|
createAt: now,
|
|
|
updateAt: now,
|
|
|
},
|
|
|
@@ -129,6 +165,14 @@ export class ChannelService {
|
|
|
}
|
|
|
|
|
|
const now = this.now();
|
|
|
+ const categoryIds = dto.categories?.map((c) => c.id) ?? [];
|
|
|
+ const tagIds = dto.tags?.map((t) => t.id) ?? [];
|
|
|
+
|
|
|
+ const [categories, tags] = await Promise.all([
|
|
|
+ this.resolveCategoriesByIds(categoryIds),
|
|
|
+ this.resolveTagsByIds(tagIds),
|
|
|
+ ]);
|
|
|
+ const tagNames = tags.map((t) => t.name.trim());
|
|
|
|
|
|
try {
|
|
|
const channel = await this.mongoPrismaService.channel.update({
|
|
|
@@ -142,9 +186,12 @@ export class ChannelService {
|
|
|
clientName: this.trimOptional(dto.clientName) ?? null,
|
|
|
clientNotice: this.trimOptional(dto.clientNotice) ?? null,
|
|
|
remark: this.trimOptional(dto.remark) ?? null,
|
|
|
- categories: (dto.categories as any) || null,
|
|
|
- tags: (dto.tags as any) || null,
|
|
|
- tagNames: dto.tagNames || [],
|
|
|
+
|
|
|
+ // 🔑 resolved by backend
|
|
|
+ categories: categories.length ? categories : null,
|
|
|
+ tags: tags.length ? tags : null,
|
|
|
+
|
|
|
+ tagNames: tagNames,
|
|
|
updateAt: now,
|
|
|
},
|
|
|
});
|