8/7/405
This commit is contained in:
@@ -5,6 +5,27 @@ import { PrismaService } from '../prisma/prisma.service';
|
||||
export class StockNotifyService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async listForCustomer(customerId: string) {
|
||||
const rows = await this.prisma.stockNotifyRequest.findMany({
|
||||
where: { customerId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
product: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
color: true,
|
||||
stock: true,
|
||||
imageUrlsJson: true,
|
||||
catalog: { select: { code: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
return rows.map((r) => this.mapCustomer(r));
|
||||
}
|
||||
|
||||
async listAdmin(activeOnly = true) {
|
||||
const rows = await this.prisma.stockNotifyRequest.findMany({
|
||||
where: activeOnly ? { active: true } : undefined,
|
||||
@@ -68,6 +89,45 @@ export class StockNotifyService {
|
||||
return this.map(row);
|
||||
}
|
||||
|
||||
private productThumb(imageUrlsJson: unknown): string {
|
||||
if (!Array.isArray(imageUrlsJson)) return '';
|
||||
const url = imageUrlsJson.find(
|
||||
(item): item is string => typeof item === 'string' && item.trim().length > 0,
|
||||
);
|
||||
return url?.trim() ?? '';
|
||||
}
|
||||
|
||||
private mapCustomer(r: {
|
||||
id: string;
|
||||
productId: string;
|
||||
active: boolean;
|
||||
createdAt: Date;
|
||||
notifiedAt: Date | null;
|
||||
product: {
|
||||
id: string;
|
||||
name: string;
|
||||
code: string;
|
||||
color: string;
|
||||
stock: number | null;
|
||||
imageUrlsJson: unknown;
|
||||
catalog: { code: string };
|
||||
};
|
||||
}) {
|
||||
return {
|
||||
id: r.id,
|
||||
productId: r.productId,
|
||||
productName: r.product.name,
|
||||
productCode: r.product.code,
|
||||
catalogCode: r.product.catalog.code,
|
||||
color: r.product.color,
|
||||
stock: r.product.stock,
|
||||
imageUrl: this.productThumb(r.product.imageUrlsJson),
|
||||
active: r.active,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
notifiedAt: r.notifiedAt?.toISOString() ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
private map(r: {
|
||||
id: string;
|
||||
customerId: string;
|
||||
|
||||
Reference in New Issue
Block a user