HospitalRepository.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import prisma from '../../prisma/PrismaClient';
  2. import { Prisma } from '@prisma/client';
  3. interface FindAllParams {
  4. skip?: number;
  5. take?: number;
  6. where?: Prisma.HospitalWhereInput;
  7. orderBy?: Prisma.HospitalOrderByWithRelationInput;
  8. }
  9. const HospitalRepository = {
  10. findAll: async ({ skip, take, where, orderBy }: FindAllParams) => {
  11. const hospitalsRaw = await prisma.hospital.findMany({
  12. where,
  13. skip,
  14. take,
  15. orderBy,
  16. select: {
  17. id: true,
  18. name: true,
  19. hospital_code: true,
  20. type: true,
  21. ownership: true,
  22. province: { select: { id: true, name: true } },
  23. city: { select: { id: true, name: true } },
  24. address: true,
  25. contact: true,
  26. image: true,
  27. progress_status: true,
  28. note: true,
  29. latitude: true,
  30. longitude: true,
  31. gmaps_url: true,
  32. // created_by: true,
  33. createdAt: true,
  34. updatedAt: true,
  35. user: { select: { id: true, fullname: true } },
  36. vendor_experiences: {
  37. where: {
  38. // status: 'active',
  39. deletedAt: null,
  40. },
  41. select: {
  42. vendor: {
  43. select: {
  44. id: true,
  45. name: true,
  46. name_pt: true,
  47. strengths: true,
  48. weaknesses: true,
  49. website: true,
  50. },
  51. },
  52. },
  53. },
  54. },
  55. });
  56. return hospitalsRaw.map(hospital => {
  57. const { vendor_experiences, ...rest } = hospital;
  58. return {
  59. ...rest,
  60. vendor: vendor_experiences?.[0]?.vendor || null,
  61. };
  62. });
  63. },
  64. countAll: async (where?: Prisma.HospitalWhereInput) => {
  65. return prisma.hospital.count({ where });
  66. },
  67. create: async (data: Prisma.HospitalCreateInput) => {
  68. return prisma.hospital.create({ data });
  69. },
  70. findById: async (id: string) => {
  71. const hospitalRaw = await prisma.hospital.findFirst({
  72. where: {
  73. id,
  74. deletedAt: null,
  75. },
  76. select: {
  77. id: true,
  78. name: true,
  79. hospital_code: true,
  80. type: true,
  81. ownership: true,
  82. province: { select: { id: true, name: true } },
  83. city: { select: { id: true, name: true } },
  84. address: true,
  85. contact: true,
  86. image: true,
  87. progress_status: true,
  88. note: true,
  89. latitude: true,
  90. longitude: true,
  91. gmaps_url: true,
  92. created_by: true,
  93. createdAt: true,
  94. updatedAt: true,
  95. user: { select: { id: true, fullname: true } },
  96. vendor_experiences: {
  97. where: {
  98. // status: 'active',
  99. deletedAt: null,
  100. },
  101. take: 1,
  102. select: {
  103. vendor: {
  104. select: {
  105. id: true,
  106. name: true,
  107. name_pt: true,
  108. strengths: true,
  109. weaknesses: true,
  110. website: true,
  111. },
  112. },
  113. },
  114. },
  115. },
  116. });
  117. if (!hospitalRaw) return null;
  118. const { vendor_experiences, ...rest } = hospitalRaw;
  119. return {
  120. ...rest,
  121. vendor: vendor_experiences?.[0]?.vendor || null,
  122. };
  123. },
  124. update: async (id: string, data: Prisma.HospitalUpdateInput) => {
  125. return prisma.hospital.update({
  126. where: { id },
  127. data,
  128. });
  129. },
  130. };
  131. export default HospitalRepository;