Files
inkreach-official-website/apps/api/src/countries/countries.service.ts
T
yeuimu 1e07c62fc8 feat(admin): country drag-sort + always-available member detail sync
- Country 加 sort_order(默认 0 保持 id 序);PATCH /countries/sort 批量保存
  顺序(对齐 /tags/sort 模式);findAll 与公开 /public/countries 均按
  sortOrder 排序
- CountriesView 表格改为可拖拽行列表:拖动松开即全量保存新顺序,失败回滚
- 商品编辑弹窗成员展开面板:同步详情按钮常驻(已同步显示 重新同步详情),
  不再只在未同步态出现
- goods.service.spec 的 FamilyRecomputeService mock 补齐 syncFamilyTags 等
  方法(全量并行时其他套件的扫名归族会把本套件夹具收进族,create/update
  会调用到,mock 缺方法导致偶发 TypeError)
- api 164/164、admin typecheck+22/22+构建全绿
2026-08-28 20:00:56 +08:00

117 lines
3.3 KiB
TypeScript

import { Prisma } from '@prisma/client';
import {
BadRequestException,
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateCountryDto } from './dto/create-country.dto';
import { UpdateCountryDto } from './dto/update-country.dto';
import { ReorderCountriesDto } from './dto/reorder-countries.dto';
@Injectable()
export class CountriesService {
constructor(private readonly prisma: PrismaService) {}
findAll() {
return this.prisma.country.findMany({
orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }],
});
}
/** 批量保存拖拽后的顺序(全量提交,sortOrder = 目标下标) */
async reorder(dto: ReorderCountriesDto) {
await this.prisma.$transaction(
dto.items.map((item) =>
this.prisma.country.update({
where: { id: BigInt(item.id) },
data: { sortOrder: item.sortOrder },
}),
),
);
return this.findAll();
}
async findOne(id: bigint) {
const country = await this.prisma.country.findUnique({ where: { id } });
if (!country) {
throw new NotFoundException(`Country ${id} not found`);
}
return country;
}
async create(dto: CreateCountryDto) {
try {
return await this.prisma.country.create({
data: {
countryName: dto.countryName,
countryIcon: dto.countryIcon ?? null,
},
});
} catch (err) {
if (
err instanceof Prisma.PrismaClientKnownRequestError &&
err.code === 'P2002'
) {
throw new ConflictException('Country name already exists');
}
throw err;
}
}
async update(id: bigint, dto: UpdateCountryDto) {
await this.findOne(id);
try {
return await this.prisma.country.update({
where: { id },
data: {
countryName: dto.countryName,
countryIcon: dto.countryIcon === undefined ? undefined : dto.countryIcon,
},
});
} catch (err) {
if (
err instanceof Prisma.PrismaClientKnownRequestError &&
err.code === 'P2002'
) {
throw new ConflictException('Country name already exists');
}
throw err;
}
}
async remove(id: bigint) {
await this.findOne(id);
try {
return await this.prisma.country.delete({ where: { id } });
} catch (err) {
if (this.isForeignKeyViolation(err)) {
throw new BadRequestException(
'Country is referenced by goods or positions and cannot be deleted',
);
}
throw err;
}
}
private isForeignKeyViolation(err: unknown): boolean {
if (err instanceof Prisma.PrismaClientKnownRequestError) {
// P2003 = FK constraint violation
return err.code === 'P2003';
}
// Fallback: Prisma sometimes surfaces FK violations as UnknownRequestError
// when the constraint check happens server-side before the typed error
// is mapped (e.g. cascading RESTRICT).
if (err instanceof Prisma.PrismaClientUnknownRequestError) {
const msg = err.message ?? '';
return (
msg.includes('foreign key constraint') ||
msg.includes('RESTRICT') ||
msg.includes('violates')
);
}
return false;
}
}