- Debian-based api image (bookworm-slim), docker/debian mirrors, prisma binaryTargets for openssl 3.0 - nginx: admin SPA under /admin, TLS via acme.sh (ZeroSSL) + auto-renewal cron, http->https redirect - prisma: add origin_goods.delisted migration, sync missing schema (good_image/tag_font_color/good_tags), fix users.createdAt Timestamptz - api: CORS wildcard reflection, helmet CORP cross-origin, price backfill in persistProductDetail, categoryIcon ancestor fallback, mediaByColor per-color gallery in public goods detail - admin: /admin base path (vite + router) - import-data.mjs: udt_name casting, serial sequence advance fix
101 lines
2.8 KiB
TypeScript
101 lines
2.8 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';
|
|
|
|
@Injectable()
|
|
export class CountriesService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
findAll() {
|
|
return this.prisma.country.findMany({ orderBy: { id: 'asc' } });
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|