12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import { Request, Response } from 'express';
- import { ListResponse } from '../../../utils/ListResponse';
- import { formatISOWithoutTimezone } from '../../../utils/FormatDate';
- import { VendorExperienceDTO } from '../../../types/sales/vendor_experience/VendorExperienceDTO';
- const formatItem = (item: VendorExperienceDTO) => ({
- ...item,
- // contract_value_min: item.contract_value_min !== null ? Number(item.contract_value_min) : null,
- // contract_value_max: item.contract_value_max !== null ? Number(item.contract_value_max) : null,
- // contract_start_date: formatDateOnly(item.contract_start_date),
- // contract_expired_date: formatDateOnly(item.contract_expired_date),
- createdAt: formatISOWithoutTimezone(item.createdAt),
- updatedAt: formatISOWithoutTimezone(item.updatedAt),
- });
- export const VendorExperienceCollection = async (
- req: Request,
- res: Response,
- data: VendorExperienceDTO[] = [],
- total: number | null = null,
- page: number = 1,
- limit: number = 10,
- message: string = 'Success'
- ): Promise<Response> => {
- const ids = data.map(item => item.id);
- // Ambil tags dari category_links yang relevan
- // const allTags = await prisma.categoryLink.findMany({
- // where: {
- // source_id: { in: ids },
- // source_type: { in: ['vendor_experience_positive_notes', 'vendor_experience_negative_notes'] },
- // deletedAt: null,
- // },
- // include: {
- // Category: true,
- // },
- // });
- // Kelompokkan tags berdasarkan source_type dan source_id
- // const tagMap = new Map<string, { positive: string[]; negative: string[] }>();
- // for (const id of ids) {
- // tagMap.set(id, { positive: [], negative: [] });
- // }
- // for (const tag of allTags) {
- // const id = tag.source_id!;
- // const categoryTag = tag.Category?.tag ?? '';
- // const current = tagMap.get(id);
- // if (!current) continue;
- // if (tag.source_type === 'vendor_experience_positive_notes') {
- // current.positive.push(categoryTag);
- // } else if (tag.source_type === 'vendor_experience_negative_notes') {
- // current.negative.push(categoryTag);
- // }
- // }
- // Gabungkan dan format
- const formattedData = data.map(item => {
- // const tags = tagMap.get(item.id);
- return formatItem({
- ...item,
- // positive_notes_tags: tags?.positive ?? [],
- // negative_notes_tags: tags?.negative ?? [],
- });
- });
- if (typeof total !== 'number') {
- return res.status(200).json({
- success: true,
- message,
- data: Array.isArray(formattedData),
- });
- }
- return ListResponse({
- req,
- res,
- data: formattedData,
- total,
- page,
- limit,
- message,
- });
- };
|