60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|||
|
|
import { Prisma } from '@prisma/client';
|
||
|
|
import { PrismaService } from '../../prisma/prisma.service';
|
||
|
|
|
||
|
|
// passwordHash must never leave the API — every customer read goes through this.
|
||
|
|
const PUBLIC_CUSTOMER_SELECT = {
|
||
|
|
id: true,
|
||
|
|
fullName: true,
|
||
|
|
mobile: true,
|
||
|
|
nationalCode: true,
|
||
|
|
cityId: true,
|
||
|
|
referredByAgentId: true,
|
||
|
|
goldCreditLimitGrams: true,
|
||
|
|
goldDebtGrams: true,
|
||
|
|
fiatDebtRial: true,
|
||
|
|
verificationStep: true,
|
||
|
|
nationalIdImageUrl: true,
|
||
|
|
businessLicenseUrl: true,
|
||
|
|
shopImageUrl: true,
|
||
|
|
isActive: true,
|
||
|
|
createdAt: true,
|
||
|
|
updatedAt: true,
|
||
|
|
} satisfies Prisma.CustomerSelect;
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class CustomersService {
|
||
|
|
constructor(private readonly prisma: PrismaService) {}
|
||
|
|
|
||
|
|
async getOrThrow(customerId: string) {
|
||
|
|
const customer = await this.prisma.customer.findUnique({
|
||
|
|
where: { id: customerId },
|
||
|
|
select: PUBLIC_CUSTOMER_SELECT,
|
||
|
|
});
|
||
|
|
if (!customer) throw new NotFoundException(`Customer ${customerId} not found`);
|
||
|
|
return customer;
|
||
|
|
}
|
||
|
|
|
||
|
|
list() {
|
||
|
|
return this.prisma.customer.findMany({
|
||
|
|
select: PUBLIC_CUSTOMER_SELECT,
|
||
|
|
orderBy: { createdAt: 'desc' },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
getOrders(customerId: string) {
|
||
|
|
return this.prisma.order.findMany({
|
||
|
|
where: { customerId },
|
||
|
|
include: { items: true },
|
||
|
|
orderBy: { createdAt: 'desc' },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
getLedgerEntries(customerId: string) {
|
||
|
|
return this.prisma.ledgerEntry.findMany({
|
||
|
|
where: { customerId },
|
||
|
|
orderBy: { createdAt: 'desc' },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|