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;
|
|
|
|
|
|
2026-09-22 00:43:11 +03:30
|
|
|
/**
|
|
|
|
|
* The only fields `GET /customers` may expose.
|
|
|
|
|
*
|
|
|
|
|
* That route is unauthenticated because both user apps' login screens download
|
|
|
|
|
* the list and match the phone number in the browser. With the full select
|
|
|
|
|
* above, an anonymous caller got the entire customer book: every national ID
|
|
|
|
|
* number, every debt and credit limit, and the URLs of every identity-document
|
|
|
|
|
* scan. Logged as SEC-08 in docs/parity/ISSUES.md.
|
|
|
|
|
*
|
|
|
|
|
* The reference never allowed this — its login was
|
|
|
|
|
* POST userAuth (SMS) -> POST userVerfiy, and the server returned exactly ONE
|
|
|
|
|
* customer after the code matched (henza_server ApiMobileController.php:39-78).
|
|
|
|
|
*
|
|
|
|
|
* Stage 2 of the security hotfix restores that flow and removes this route from
|
|
|
|
|
* the public surface entirely. Until then these four fields are what the login
|
|
|
|
|
* screens actually read (LoginView.vue:131-133 matches on mobile/isActive/
|
|
|
|
|
* isArchived, then calls session.login(id, mobile)) — nothing more.
|
|
|
|
|
*/
|
|
|
|
|
const LOGIN_LOOKUP_SELECT = {
|
|
|
|
|
id: true,
|
|
|
|
|
mobile: true,
|
|
|
|
|
isActive: true,
|
|
|
|
|
isArchived: true,
|
|
|
|
|
} satisfies Prisma.CustomerSelect;
|
|
|
|
|
|
2026-09-09 06:46:27 +03:30
|
|
|
@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-22 00:43:11 +03:30
|
|
|
select: LOGIN_LOOKUP_SELECT,
|
2026-09-09 06:46:27 +03:30
|
|
|
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' },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|