VendorExperienceCollection.ts 2.9 KB

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