Files
dukang/server/dukang-api/src/modules/iam/user-address.service.ts
T
2026-07-06 13:18:56 +08:00

82 lines
3.0 KiB
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../common/prisma/prisma.module';
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
@Injectable()
export class UserAddressService {
constructor(private readonly prisma: PrismaService) {}
async list(userId: bigint) {
const list = await this.prisma.userAddress.findMany({
where: { userId },
orderBy: [{ isDefault: 'desc' }, { updatedAt: 'desc' }],
});
return serializeBigInt(list);
}
async normalizeDefaultAddress(userId: bigint) {
const defaults = await this.prisma.userAddress.findMany({
where: { userId, isDefault: 1 },
orderBy: { updatedAt: 'desc' },
});
if (defaults.length <= 1) return;
const keep = defaults[0];
await this.prisma.$transaction(async (tx) => {
await tx.userAddress.updateMany({ where: { userId }, data: { isDefault: 0 } });
await tx.userAddress.update({ where: { id: keep.id }, data: { isDefault: 1 } });
});
}
async create(userId: bigint, body: Record<string, unknown>) {
const isDefault = body.isDefault ? 1 : 0;
const address = await this.prisma.$transaction(async (tx) => {
if (isDefault) {
await tx.userAddress.updateMany({ where: { userId }, data: { isDefault: 0 } });
}
return tx.userAddress.create({
data: {
userId,
receiverName: String(body.receiverName),
phone: String(body.phone),
province: String(body.province),
city: String(body.city),
district: String(body.district),
detail: String(body.detail),
isDefault,
},
});
});
return serializeBigInt(address);
}
async update(userId: bigint, id: bigint, body: Record<string, unknown>) {
const existing = await this.prisma.userAddress.findFirst({ where: { id, userId } });
if (!existing) throw new NotFoundException('地址不存在');
const address = await this.prisma.$transaction(async (tx) => {
if (body.isDefault) {
await tx.userAddress.updateMany({ where: { userId }, data: { isDefault: 0 } });
}
return tx.userAddress.update({
where: { id },
data: {
receiverName: body.receiverName ? String(body.receiverName) : undefined,
phone: body.phone ? String(body.phone) : undefined,
province: body.province ? String(body.province) : undefined,
city: body.city ? String(body.city) : undefined,
district: body.district ? String(body.district) : undefined,
detail: body.detail ? String(body.detail) : undefined,
isDefault: body.isDefault ? 1 : undefined,
},
});
});
return serializeBigInt(address);
}
async remove(userId: bigint, id: bigint) {
const existing = await this.prisma.userAddress.findFirst({ where: { id, userId } });
if (!existing) throw new NotFoundException('地址不存在');
await this.prisma.userAddress.delete({ where: { id } });
return { deleted: true };
}
}