2026-09-09 06:46:27 +03:30
|
|
|
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,
|
2026-09-21 17:37:26 +03:30
|
|
|
isArchived: true,
|
2026-09-09 06:46:27 +03:30
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-21 17:37:26 +03:30
|
|
|
// Ported from legacy henza_server's WebApp\AuthController::userAuth() /
|
|
|
|
|
// ApiMobileController::userAuth(), both of which query
|
|
|
|
|
// Customer::active()->where('is_archive', 0) — an inactive or archived
|
|
|
|
|
// customer cannot authenticate in the customer-facing app. This endpoint is
|
|
|
|
|
// that same lookup surface for the user-app login flow (client-side match
|
|
|
|
|
// on `mobile`), so it must apply the same two filters, or a
|
|
|
|
|
// deactivated/archived customer would still be able to log in.
|
2026-09-09 06:46:27 +03:30
|
|
|
list() {
|
|
|
|
|
return this.prisma.customer.findMany({
|
2026-09-21 17:37:26 +03:30
|
|
|
where: { isActive: true, isArchived: false },
|
2026-09-09 06:46:27 +03:30
|
|
|
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' },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|