VendorExperienceCollection.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Request, Response } from 'express';
  2. import { ListResponse } from '../../../utils/ListResponse';
  3. import { formatISOWithoutTimezone } from '../../../utils/FormatDate';
  4. import { VendorExperienceDTO } from '../../../types/sales/vendor_experience/VendorExperienceDTO';
  5. const formatItem = (item: VendorExperienceDTO) => ({
  6. ...item,
  7. // contract_value_min: item.contract_value_min !== null ? Number(item.contract_value_min) : null,
  8. // contract_value_max: item.contract_value_max !== null ? Number(item.contract_value_max) : null,
  9. // contract_start_date: formatDateOnly(item.contract_start_date),
  10. // contract_expired_date: formatDateOnly(item.contract_expired_date),
  11. createdAt: formatISOWithoutTimezone(item.createdAt),
  12. updatedAt: formatISOWithoutTimezone(item.updatedAt),
  13. });
  14. export const VendorExperienceCollection = async (
  15. req: Request,
  16. res: Response,
  17. data: VendorExperienceDTO[] = [],
  18. total: number | null = null,
  19. page: number = 1,
  20. limit: number = 10,
  21. message: string = 'Success'
  22. ): Promise<Response> => {
  23. const ids = data.map(item => item.id);
  24. // Ambil tags dari category_links yang relevan
  25. // const allTags = await prisma.categoryLink.findMany({
  26. // where: {
  27. // source_id: { in: ids },
  28. // source_type: { in: ['vendor_experience_positive_notes', 'vendor_experience_negative_notes'] },
  29. // deletedAt: null,
  30. // },
  31. // include: {
  32. // Category: true,
  33. // },
  34. // });
  35. // Kelompokkan tags berdasarkan source_type dan source_id
  36. // const tagMap = new Map<string, { positive: string[]; negative: string[] }>();
  37. // for (const id of ids) {
  38. // tagMap.set(id, { positive: [], negative: [] });
  39. // }
  40. // for (const tag of allTags) {
  41. // const id = tag.source_id!;
  42. // const categoryTag = tag.Category?.tag ?? '';
  43. // const current = tagMap.get(id);
  44. // if (!current) continue;
  45. // if (tag.source_type === 'vendor_experience_positive_notes') {
  46. // current.positive.push(categoryTag);
  47. // } else if (tag.source_type === 'vendor_experience_negative_notes') {
  48. // current.negative.push(categoryTag);
  49. // }
  50. // }
  51. // Gabungkan dan format
  52. const formattedData = data.map(item => {
  53. // const tags = tagMap.get(item.id);
  54. return formatItem({
  55. ...item,
  56. // positive_notes_tags: tags?.positive ?? [],
  57. // negative_notes_tags: tags?.negative ?? [],
  58. });
  59. });
  60. if (typeof total !== 'number') {
  61. return res.status(200).json({
  62. success: true,
  63. message,
  64. data: Array.isArray(formattedData),
  65. });
  66. }
  67. return ListResponse({
  68. req,
  69. res,
  70. data: formattedData,
  71. total,
  72. page,
  73. limit,
  74. message,
  75. });
  76. };