@tomei/rental 0.17.3-dev.4 → 0.17.3-dev.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (349) hide show
  1. package/.commitlintrc.json +22 -22
  2. package/.gitlab-ci.yml +16 -16
  3. package/.husky/commit-msg +9 -9
  4. package/.husky/pre-commit +7 -7
  5. package/.prettierrc +4 -4
  6. package/Jenkinsfile +51 -51
  7. package/README.md +8 -8
  8. package/dist/ClassMappings/ItemClassMap.d.ts +4 -0
  9. package/dist/ClassMappings/ItemClassMap.js +8 -0
  10. package/dist/ClassMappings/ItemClassMap.js.map +1 -0
  11. package/dist/ClassMappings/index.d.ts +2 -0
  12. package/dist/ClassMappings/index.js +6 -0
  13. package/dist/ClassMappings/index.js.map +1 -0
  14. package/dist/components/agreement/agreement.d.ts +27 -0
  15. package/dist/components/agreement/agreement.js +120 -0
  16. package/dist/components/agreement/agreement.js.map +1 -0
  17. package/dist/components/agreement/agreement.repository.d.ts +8 -0
  18. package/dist/components/agreement/agreement.repository.js +52 -0
  19. package/dist/components/agreement/agreement.repository.js.map +1 -0
  20. package/dist/components/agreement-history/agreement-history.d.ts +17 -0
  21. package/dist/components/agreement-history/agreement-history.js +40 -0
  22. package/dist/components/agreement-history/agreement-history.js.map +1 -0
  23. package/dist/components/agreement-history/agreement-history.repository.d.ts +8 -0
  24. package/dist/components/agreement-history/agreement-history.repository.js +52 -0
  25. package/dist/components/agreement-history/agreement-history.repository.js.map +1 -0
  26. package/dist/components/agreement-signature/agreement-signature.d.ts +27 -0
  27. package/dist/components/agreement-signature/agreement-signature.js +99 -0
  28. package/dist/components/agreement-signature/agreement-signature.js.map +1 -0
  29. package/dist/components/agreement-signature/agreement-signature.repository.d.ts +8 -0
  30. package/dist/components/agreement-signature/agreement-signature.repository.js +53 -0
  31. package/dist/components/agreement-signature/agreement-signature.repository.js.map +1 -0
  32. package/dist/components/booking/booking.d.ts +46 -0
  33. package/dist/components/booking/booking.js +289 -0
  34. package/dist/components/booking/booking.js.map +1 -0
  35. package/dist/components/booking/booking.repository.d.ts +8 -0
  36. package/dist/components/booking/booking.repository.js +52 -0
  37. package/dist/components/booking/booking.repository.js.map +1 -0
  38. package/dist/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.d.ts +32 -0
  39. package/dist/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js +106 -0
  40. package/dist/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js.map +1 -0
  41. package/dist/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.d.ts +9 -0
  42. package/dist/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js +62 -0
  43. package/dist/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js.map +1 -0
  44. package/dist/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.d.ts +22 -0
  45. package/dist/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js +89 -0
  46. package/dist/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js.map +1 -0
  47. package/dist/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.d.ts +9 -0
  48. package/dist/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js +63 -0
  49. package/dist/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js.map +1 -0
  50. package/dist/components/hirer-change-request-signature/hirer-change-request-signature.d.ts +30 -0
  51. package/dist/components/hirer-change-request-signature/hirer-change-request-signature.js +208 -0
  52. package/dist/components/hirer-change-request-signature/hirer-change-request-signature.js.map +1 -0
  53. package/dist/components/hirer-change-request-signature/hirer-change-request-signature.repository.d.ts +9 -0
  54. package/dist/components/hirer-change-request-signature/hirer-change-request-signature.repository.js +63 -0
  55. package/dist/components/hirer-change-request-signature/hirer-change-request-signature.repository.js.map +1 -0
  56. package/dist/components/joint-hirer/joint-hirer.d.ts +26 -0
  57. package/dist/components/joint-hirer/joint-hirer.js +137 -0
  58. package/dist/components/joint-hirer/joint-hirer.js.map +1 -0
  59. package/dist/components/joint-hirer/joint-hirer.repository.d.ts +8 -0
  60. package/dist/components/joint-hirer/joint-hirer.repository.js +52 -0
  61. package/dist/components/joint-hirer/joint-hirer.repository.js.map +1 -0
  62. package/dist/components/rental/rental.d.ts +71 -0
  63. package/dist/components/rental/rental.js +841 -0
  64. package/dist/components/rental/rental.js.map +1 -0
  65. package/dist/components/rental/rental.repository.d.ts +8 -0
  66. package/dist/components/rental/rental.repository.js +52 -0
  67. package/dist/components/rental/rental.repository.js.map +1 -0
  68. package/dist/components/rental-hirer-change-request/rental-hirer-change-request.d.ts +71 -0
  69. package/dist/components/rental-hirer-change-request/rental-hirer-change-request.js +531 -0
  70. package/dist/components/rental-hirer-change-request/rental-hirer-change-request.js.map +1 -0
  71. package/dist/components/rental-hirer-change-request/rental-hirer-change-request.repository.d.ts +9 -0
  72. package/dist/components/rental-hirer-change-request/rental-hirer-change-request.repository.js +62 -0
  73. package/dist/components/rental-hirer-change-request/rental-hirer-change-request.repository.js.map +1 -0
  74. package/dist/components/rental-price/rental-price.d.ts +18 -0
  75. package/dist/components/rental-price/rental-price.js +66 -0
  76. package/dist/components/rental-price/rental-price.js.map +1 -0
  77. package/dist/components/rental-price/rental-price.repository.d.ts +8 -0
  78. package/dist/components/rental-price/rental-price.repository.js +52 -0
  79. package/dist/components/rental-price/rental-price.repository.js.map +1 -0
  80. package/dist/database.d.ts +4 -0
  81. package/dist/database.js +37 -0
  82. package/dist/database.js.map +1 -0
  83. package/dist/enum/account-type.enum.d.ts +4 -0
  84. package/dist/enum/account-type.enum.js +9 -0
  85. package/dist/enum/account-type.enum.js.map +1 -0
  86. package/dist/enum/aggrement-status.enum.d.ts +5 -0
  87. package/dist/enum/aggrement-status.enum.js +10 -0
  88. package/dist/enum/aggrement-status.enum.js.map +1 -0
  89. package/dist/enum/agreement-signature-status.enum.d.ts +4 -0
  90. package/dist/enum/agreement-signature-status.enum.js +9 -0
  91. package/dist/enum/agreement-signature-status.enum.js.map +1 -0
  92. package/dist/enum/booking.enum.d.ts +5 -0
  93. package/dist/enum/booking.enum.js +10 -0
  94. package/dist/enum/booking.enum.js.map +1 -0
  95. package/dist/enum/hirer-type.enum.d.ts +4 -0
  96. package/dist/enum/hirer-type.enum.js +9 -0
  97. package/dist/enum/hirer-type.enum.js.map +1 -0
  98. package/dist/enum/index.d.ts +10 -0
  99. package/dist/enum/index.js +22 -0
  100. package/dist/enum/index.js.map +1 -0
  101. package/dist/enum/rental-hirer-change-request-hirer-role.d.ts +4 -0
  102. package/dist/enum/rental-hirer-change-request-hirer-role.js +9 -0
  103. package/dist/enum/rental-hirer-change-request-hirer-role.js.map +1 -0
  104. package/dist/enum/rental-hirer-change-request-status.d.ts +7 -0
  105. package/dist/enum/rental-hirer-change-request-status.js +12 -0
  106. package/dist/enum/rental-hirer-change-request-status.js.map +1 -0
  107. package/dist/enum/rental-hirer-change-request-type.d.ts +4 -0
  108. package/dist/enum/rental-hirer-change-request-type.js +9 -0
  109. package/dist/enum/rental-hirer-change-request-type.js.map +1 -0
  110. package/dist/enum/rental-status.enum.d.ts +9 -0
  111. package/dist/enum/rental-status.enum.js +14 -0
  112. package/dist/enum/rental-status.enum.js.map +1 -0
  113. package/dist/index.d.ts +28 -1
  114. package/dist/index.js +52 -1
  115. package/dist/index.js.map +1 -1
  116. package/dist/interfaces/agreemeent-signature-attr.interface.d.ts +14 -0
  117. package/dist/interfaces/agreemeent-signature-attr.interface.js +3 -0
  118. package/dist/interfaces/agreemeent-signature-attr.interface.js.map +1 -0
  119. package/dist/interfaces/agreement-attr.interface.d.ts +7 -0
  120. package/dist/{src/interfaces/hirer-signature-attr.interface.js → interfaces/agreement-attr.interface.js} +1 -1
  121. package/dist/interfaces/agreement-attr.interface.js.map +1 -0
  122. package/dist/interfaces/agreement-history-attr.interface.d.ts +7 -0
  123. package/dist/interfaces/agreement-history-attr.interface.js +3 -0
  124. package/dist/interfaces/agreement-history-attr.interface.js.map +1 -0
  125. package/dist/interfaces/booking-attr.interface.d.ts +18 -0
  126. package/dist/interfaces/booking-attr.interface.js +3 -0
  127. package/dist/interfaces/booking-attr.interface.js.map +1 -0
  128. package/dist/interfaces/booking-find-all-search-attr.interface.d.ts +12 -0
  129. package/dist/interfaces/booking-find-all-search-attr.interface.js +3 -0
  130. package/dist/interfaces/booking-find-all-search-attr.interface.js.map +1 -0
  131. package/dist/interfaces/hirer-change-request-new-hirer-attr.interface.d.ts +15 -0
  132. package/dist/interfaces/hirer-change-request-new-hirer-attr.interface.js +3 -0
  133. package/dist/interfaces/hirer-change-request-new-hirer-attr.interface.js.map +1 -0
  134. package/dist/interfaces/hirer-change-request-remove-hirer-attr.interface.d.ts +5 -0
  135. package/dist/interfaces/hirer-change-request-remove-hirer-attr.interface.js +3 -0
  136. package/dist/interfaces/hirer-change-request-remove-hirer-attr.interface.js.map +1 -0
  137. package/dist/interfaces/hirer-change-request-signature-attr.interface.d.ts +14 -0
  138. package/dist/interfaces/hirer-change-request-signature-attr.interface.js +3 -0
  139. package/dist/interfaces/hirer-change-request-signature-attr.interface.js.map +1 -0
  140. package/dist/interfaces/index.d.ts +14 -0
  141. package/dist/interfaces/index.js +3 -0
  142. package/dist/interfaces/index.js.map +1 -0
  143. package/dist/interfaces/joint-hirer-attr.interface.d.ts +11 -0
  144. package/dist/interfaces/joint-hirer-attr.interface.js +3 -0
  145. package/dist/interfaces/joint-hirer-attr.interface.js.map +1 -0
  146. package/dist/interfaces/rental-attr.interface.d.ts +24 -0
  147. package/dist/interfaces/rental-attr.interface.js +3 -0
  148. package/dist/interfaces/rental-attr.interface.js.map +1 -0
  149. package/dist/interfaces/rental-find-all-search-attr.interface.d.ts +10 -0
  150. package/dist/interfaces/rental-find-all-search-attr.interface.js +3 -0
  151. package/dist/interfaces/rental-find-all-search-attr.interface.js.map +1 -0
  152. package/dist/interfaces/rental-hirer-change-request.attr.interface.d.ts +16 -0
  153. package/dist/interfaces/rental-hirer-change-request.attr.interface.js +3 -0
  154. package/dist/interfaces/rental-hirer-change-request.attr.interface.js.map +1 -0
  155. package/dist/interfaces/rental-hirer-change-request.update.interface.d.ts +4 -0
  156. package/dist/interfaces/rental-hirer-change-request.update.interface.js +3 -0
  157. package/dist/interfaces/rental-hirer-change-request.update.interface.js.map +1 -0
  158. package/dist/interfaces/rental-price-attr.interface.d.ts +7 -0
  159. package/dist/interfaces/rental-price-attr.interface.js +3 -0
  160. package/dist/interfaces/rental-price-attr.interface.js.map +1 -0
  161. package/dist/interfaces/response-hirer-signature-attr.interface.d.ts +14 -0
  162. package/dist/interfaces/response-hirer-signature-attr.interface.js +3 -0
  163. package/dist/interfaces/response-hirer-signature-attr.interface.js.map +1 -0
  164. package/dist/models/agreement-history.entity.d.ts +10 -0
  165. package/dist/models/agreement-history.entity.js +65 -0
  166. package/dist/models/agreement-history.entity.js.map +1 -0
  167. package/dist/models/agreement-signature.entity.d.ts +18 -0
  168. package/dist/{src/models/hirer-signature.entity.js → models/agreement-signature.entity.js} +7 -6
  169. package/dist/models/agreement-signature.entity.js.map +1 -0
  170. package/dist/models/agreement.entity.d.ts +14 -0
  171. package/dist/models/agreement.entity.js +70 -0
  172. package/dist/models/agreement.entity.js.map +1 -0
  173. package/dist/models/booking.entity.d.ts +21 -0
  174. package/dist/models/booking.entity.js +128 -0
  175. package/dist/models/booking.entity.js.map +1 -0
  176. package/dist/models/hirer-change-request-new-hirer.entity.d.ts +19 -0
  177. package/dist/models/hirer-change-request-new-hirer.entity.js +121 -0
  178. package/dist/models/hirer-change-request-new-hirer.entity.js.map +1 -0
  179. package/dist/models/hirer-change-request-remove-hirer.entity.d.ts +11 -0
  180. package/dist/models/hirer-change-request-remove-hirer.entity.js +57 -0
  181. package/dist/models/hirer-change-request-remove-hirer.entity.js.map +1 -0
  182. package/dist/models/hirer-change-request-signature.entity.d.ts +18 -0
  183. package/dist/models/hirer-change-request-signature.entity.js +101 -0
  184. package/dist/models/hirer-change-request-signature.entity.js.map +1 -0
  185. package/dist/models/index.d.ts +12 -0
  186. package/dist/models/index.js +26 -0
  187. package/dist/models/index.js.map +1 -0
  188. package/dist/models/joint-hirer.entity.d.ts +14 -0
  189. package/dist/models/joint-hirer.entity.js +91 -0
  190. package/dist/models/joint-hirer.entity.js.map +1 -0
  191. package/dist/models/rental-hirer-change-request.entity.d.ts +22 -0
  192. package/dist/models/rental-hirer-change-request.entity.js +108 -0
  193. package/dist/models/rental-hirer-change-request.entity.js.map +1 -0
  194. package/dist/models/rental-price.entity.d.ts +8 -0
  195. package/dist/models/rental-price.entity.js +59 -0
  196. package/dist/models/rental-price.entity.js.map +1 -0
  197. package/dist/models/rental.entity.d.ts +29 -0
  198. package/dist/models/rental.entity.js +160 -0
  199. package/dist/models/rental.entity.js.map +1 -0
  200. package/dist/src/components/agreement/agreement.d.ts +1 -1
  201. package/dist/src/components/agreement/agreement.js +77 -94
  202. package/dist/src/components/agreement/agreement.js.map +1 -1
  203. package/dist/src/components/agreement/agreement.repository.js +35 -50
  204. package/dist/src/components/agreement/agreement.repository.js.map +1 -1
  205. package/dist/src/components/agreement-history/agreement-history.js +10 -21
  206. package/dist/src/components/agreement-history/agreement-history.js.map +1 -1
  207. package/dist/src/components/agreement-history/agreement-history.repository.js +35 -50
  208. package/dist/src/components/agreement-history/agreement-history.repository.js.map +1 -1
  209. package/dist/src/components/agreement-signature/agreement-signature.js +57 -70
  210. package/dist/src/components/agreement-signature/agreement-signature.js.map +1 -1
  211. package/dist/src/components/agreement-signature/agreement-signature.repository.js +36 -51
  212. package/dist/src/components/agreement-signature/agreement-signature.repository.js.map +1 -1
  213. package/dist/src/components/booking/booking.js +206 -231
  214. package/dist/src/components/booking/booking.js.map +1 -1
  215. package/dist/src/components/booking/booking.repository.js +35 -50
  216. package/dist/src/components/booking/booking.repository.js.map +1 -1
  217. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js +47 -62
  218. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js.map +1 -1
  219. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js +46 -63
  220. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js.map +1 -1
  221. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js +49 -64
  222. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js.map +1 -1
  223. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js +47 -64
  224. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js.map +1 -1
  225. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.js +150 -167
  226. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.js.map +1 -1
  227. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.js +47 -64
  228. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.js.map +1 -1
  229. package/dist/src/components/joint-hirer/joint-hirer.js +82 -97
  230. package/dist/src/components/joint-hirer/joint-hirer.js.map +1 -1
  231. package/dist/src/components/joint-hirer/joint-hirer.repository.js +35 -50
  232. package/dist/src/components/joint-hirer/joint-hirer.repository.js.map +1 -1
  233. package/dist/src/components/rental/rental.d.ts +2 -2
  234. package/dist/src/components/rental/rental.js +566 -595
  235. package/dist/src/components/rental/rental.js.map +1 -1
  236. package/dist/src/components/rental/rental.repository.js +35 -50
  237. package/dist/src/components/rental/rental.repository.js.map +1 -1
  238. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.js +416 -456
  239. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.js.map +1 -1
  240. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.js +46 -63
  241. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.js.map +1 -1
  242. package/dist/src/components/rental-price/rental-price.js +33 -46
  243. package/dist/src/components/rental-price/rental-price.js.map +1 -1
  244. package/dist/src/components/rental-price/rental-price.repository.js +35 -50
  245. package/dist/src/components/rental-price/rental-price.repository.js.map +1 -1
  246. package/dist/src/models/hirer-change-request-new-hirer.entity.d.ts +1 -1
  247. package/dist/src/models/hirer-change-request-remove-hirer.entity.d.ts +1 -1
  248. package/dist/src/models/hirer-change-request-signature.entity.d.ts +1 -1
  249. package/dist/tsconfig.build.tsbuildinfo +1 -0
  250. package/dist/tsconfig.tsbuildinfo +1 -1
  251. package/eslint.config.mjs +58 -58
  252. package/jest.config.js +10 -10
  253. package/migrations/20250529092130-add-status-to-joint-hirer.js +19 -19
  254. package/migrations/agreement-signature-table-migration.js +64 -64
  255. package/migrations/booking-table-migration.js +79 -79
  256. package/migrations/hirer-change-request-new-hirer-migration.js +72 -72
  257. package/migrations/hirer-change-request-remove-hirer-migration.js +39 -39
  258. package/migrations/hirer-change-request-signature-migration.js +65 -65
  259. package/migrations/joint-hirer-table-migration.js +52 -52
  260. package/migrations/rental-aggreement-history-migration.js +41 -41
  261. package/migrations/rental-aggrement-table-migration.js +30 -30
  262. package/migrations/rental-hirer-change-request-migrations.js +64 -64
  263. package/migrations/rental-price-table-migration.js +32 -32
  264. package/migrations/rental-table-migrations.js +96 -96
  265. package/package.json +77 -75
  266. package/sonar-project.properties +12 -12
  267. package/src/ClassMappings/ItemClassMap.ts +7 -0
  268. package/src/ClassMappings/index.ts +3 -0
  269. package/src/components/agreement/agreement.repository.ts +54 -54
  270. package/src/components/agreement/agreement.ts +185 -185
  271. package/src/components/agreement-history/agreement-history.repository.ts +54 -54
  272. package/src/components/agreement-history/agreement-history.ts +57 -57
  273. package/src/components/agreement-signature/agreement-signature.repository.ts +55 -55
  274. package/src/components/agreement-signature/agreement-signature.ts +143 -143
  275. package/src/components/booking/booking.repository.ts +51 -51
  276. package/src/components/booking/booking.ts +492 -492
  277. package/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.ts +64 -64
  278. package/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.ts +153 -153
  279. package/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.ts +65 -65
  280. package/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.ts +134 -134
  281. package/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.ts +65 -65
  282. package/src/components/hirer-change-request-signature/hirer-change-request-signature.ts +272 -272
  283. package/src/components/joint-hirer/joint-hirer.repository.ts +54 -54
  284. package/src/components/joint-hirer/joint-hirer.ts +189 -189
  285. package/src/components/rental/rental.repository.ts +51 -51
  286. package/src/components/rental/rental.ts +1395 -1167
  287. package/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.ts +64 -64
  288. package/src/components/rental-hirer-change-request/rental-hirer-change-request.ts +917 -918
  289. package/src/components/rental-price/rental-price.repository.ts +54 -54
  290. package/src/components/rental-price/rental-price.ts +100 -100
  291. package/src/database.ts +39 -39
  292. package/src/enum/account-type.enum.ts +4 -4
  293. package/src/enum/agreement-signature-status.enum.ts +4 -4
  294. package/src/enum/booking.enum.ts +5 -5
  295. package/src/enum/hirer-type.enum.ts +4 -4
  296. package/src/enum/index.ts +21 -21
  297. package/src/enum/rental-hirer-change-request-hirer-role.ts +4 -4
  298. package/src/enum/rental-hirer-change-request-status.ts +7 -7
  299. package/src/enum/rental-hirer-change-request-type.ts +4 -4
  300. package/src/enum/rental-status.enum.ts +39 -39
  301. package/src/index.ts +54 -52
  302. package/src/interfaces/agreemeent-signature-attr.interface.ts +15 -15
  303. package/src/interfaces/agreement-attr.interface.ts +7 -7
  304. package/src/interfaces/agreement-history-attr.interface.ts +7 -7
  305. package/src/interfaces/booking-attr.interface.ts +19 -19
  306. package/src/interfaces/booking-find-all-search-attr.interface.ts +12 -12
  307. package/src/interfaces/hirer-change-request-new-hirer-attr.interface.ts +15 -15
  308. package/src/interfaces/hirer-change-request-remove-hirer-attr.interface.ts +5 -5
  309. package/src/interfaces/hirer-change-request-signature-attr.interface.ts +15 -15
  310. package/src/interfaces/index.ts +29 -29
  311. package/src/interfaces/joint-hirer-attr.interface.ts +11 -11
  312. package/src/interfaces/rental-attr.interface.ts +25 -25
  313. package/src/interfaces/rental-find-all-search-attr.interface.ts +11 -11
  314. package/src/interfaces/rental-hirer-change-request.attr.interface.ts +17 -17
  315. package/src/interfaces/rental-hirer-change-request.update.interface.ts +4 -4
  316. package/src/interfaces/rental-price-attr.interface.ts +7 -7
  317. package/src/interfaces/response-hirer-signature-attr.interface.ts +15 -15
  318. package/src/models/agreement-history.entity.ts +51 -51
  319. package/src/models/agreement-signature.entity.ts +86 -86
  320. package/src/models/agreement.entity.ts +47 -47
  321. package/src/models/booking.entity.ts +105 -105
  322. package/src/models/hirer-change-request-new-hirer.entity.ts +102 -102
  323. package/src/models/hirer-change-request-remove-hirer.entity.ts +47 -47
  324. package/src/models/hirer-change-request-signature.entity.ts +86 -86
  325. package/src/models/index.ts +25 -25
  326. package/src/models/joint-hirer.entity.ts +70 -70
  327. package/src/models/rental-price.entity.ts +38 -38
  328. package/src/models/rental.entity.ts +133 -133
  329. package/tsconfig.build.json +11 -6
  330. package/tsconfig.json +22 -24
  331. package/dist/src/components/agreement-signature/hirer-signature.d.ts +0 -27
  332. package/dist/src/components/agreement-signature/hirer-signature.js +0 -112
  333. package/dist/src/components/agreement-signature/hirer-signature.js.map +0 -1
  334. package/dist/src/components/agreement-signature/hirer-signature.repository.d.ts +0 -8
  335. package/dist/src/components/agreement-signature/hirer-signature.repository.js +0 -67
  336. package/dist/src/components/agreement-signature/hirer-signature.repository.js.map +0 -1
  337. package/dist/src/components/hirer-signature/hirer-signature.d.ts +0 -27
  338. package/dist/src/components/hirer-signature/hirer-signature.js +0 -112
  339. package/dist/src/components/hirer-signature/hirer-signature.js.map +0 -1
  340. package/dist/src/components/hirer-signature/hirer-signature.repository.d.ts +0 -8
  341. package/dist/src/components/hirer-signature/hirer-signature.repository.js +0 -67
  342. package/dist/src/components/hirer-signature/hirer-signature.repository.js.map +0 -1
  343. package/dist/src/enum/hirer-signature-status.enum.d.ts +0 -4
  344. package/dist/src/enum/hirer-signature-status.enum.js +0 -9
  345. package/dist/src/enum/hirer-signature-status.enum.js.map +0 -1
  346. package/dist/src/interfaces/hirer-signature-attr.interface.d.ts +0 -14
  347. package/dist/src/interfaces/hirer-signature-attr.interface.js.map +0 -1
  348. package/dist/src/models/hirer-signature.entity.d.ts +0 -16
  349. package/dist/src/models/hirer-signature.entity.js.map +0 -1
@@ -1,1167 +1,1395 @@
1
- import { IRentalAttr } from '../../interfaces/rental-attr.interface';
2
- import { RentalStatusEnum } from '../../enum/rental-status.enum';
3
- import { RentalRepository } from './rental.repository';
4
- import { ClassError, ObjectBase } from '@tomei/general';
5
- import { ApplicationConfig } from '@tomei/config';
6
- import { LoginUser } from '@tomei/sso';
7
- import { RentalPrice } from '../rental-price/rental-price';
8
- import { Op, QueryTypes } from 'sequelize';
9
- import { ActionEnum, Activity } from '@tomei/activity-history';
10
- import { IRentalFindAllSearchAttr } from '../../interfaces/rental-find-all-search-attr.interface';
11
- import { RentalAccountTypeEnum } from '../../enum/account-type.enum';
12
- import { JointHirer } from '../joint-hirer/joint-hirer';
13
- import { JointHirerModel } from '../../models/joint-hirer.entity';
14
- import { AgreementRepository } from '../agreement/agreement.repository';
15
- import * as rentalDb from '../../database';
16
- import { RentalModel } from '../../models/rental.entity';
17
- import { JointHirerRepository } from '../joint-hirer/joint-hirer.repository';
18
- import { AgreementModel } from '../../models';
19
- import { Agreement } from '../agreement/agreement';
20
- import { AggrementStatusEnum } from '../../enum/aggrement-status.enum';
21
- import { AgreementSignatureRepository } from '../agreement-signature/agreement-signature.repository';
22
- import { AgreementSignatureStatusEnum } from '../../enum/agreement-signature-status.enum';
23
- import { HirerTypeEnum } from '../../enum/hirer-type.enum';
24
- import { AgreementHistoryRepository } from '../agreement-history/agreement-history.repository';
25
-
26
- export class Rental extends ObjectBase {
27
- ObjectId: string;
28
- ObjectName: string;
29
- ObjectType = 'Rental';
30
- TableName: string;
31
- CustomerId: string;
32
- CustomerType: string;
33
- ItemId: string;
34
- ItemType: string;
35
- PriceId: string;
36
- StartDateTime: Date;
37
- EndDateTime: Date;
38
- CancelRemarks: string;
39
- TerminateRemarks: string;
40
- AgreementNo: string;
41
- AccountType: RentalAccountTypeEnum;
42
- JointHirers: JointHirer[] = [];
43
- protected _Status: RentalStatusEnum | string;
44
- protected _EscheatmentYN: string = 'N';
45
- protected _CreatedById: string;
46
- protected _CreatedAt: Date;
47
- protected _UpdatedById: string;
48
- protected _UpdatedAt: Date;
49
- protected static _Repo = new RentalRepository();
50
- protected static _AgreementRepo = new AgreementRepository();
51
- protected static _JointHirerRepo = new JointHirerRepository();
52
- protected static _AgreementSignatureRepo = new AgreementSignatureRepository();
53
- protected static _AgreementHistoryRepo = new AgreementHistoryRepository();
54
-
55
- get RentalId(): string {
56
- return this.ObjectId;
57
- }
58
-
59
- set RentalId(value: string) {
60
- this.ObjectId = value;
61
- }
62
-
63
- get Status(): RentalStatusEnum | string {
64
- return this._Status;
65
- }
66
-
67
- get EscheatmentYN(): string {
68
- return this._EscheatmentYN;
69
- }
70
-
71
- get CreatedById(): string {
72
- return this._CreatedById;
73
- }
74
-
75
- get CreatedAt(): Date {
76
- return this._CreatedAt;
77
- }
78
-
79
- get UpdatedById(): string {
80
- return this._UpdatedById;
81
- }
82
-
83
- get UpdatedAt(): Date {
84
- return this._UpdatedAt;
85
- }
86
-
87
- private setTerminated() {
88
- this._Status = RentalStatusEnum.TERMINATED;
89
- }
90
-
91
- protected constructor(rentalAttr?: IRentalAttr) {
92
- super();
93
- if (rentalAttr) {
94
- this.RentalId = rentalAttr.RentalId;
95
- this.CustomerId = rentalAttr.CustomerId;
96
- this.CustomerType = rentalAttr.CustomerType;
97
- this.ItemId = rentalAttr.ItemId;
98
- this.ItemType = rentalAttr.ItemType;
99
- this.PriceId = rentalAttr.PriceId;
100
- this.StartDateTime = rentalAttr.StartDateTime;
101
- this.EndDateTime = rentalAttr.EndDateTime;
102
- this.CancelRemarks = rentalAttr.CancelRemarks;
103
- this.TerminateRemarks = rentalAttr.TerminateRemarks;
104
- this.AgreementNo = rentalAttr.AgreementNo;
105
- this.AccountType = rentalAttr.AccountType;
106
- this._Status = rentalAttr.Status;
107
- this._EscheatmentYN = rentalAttr.EscheatmentYN;
108
- this._CreatedById = rentalAttr.CreatedById;
109
- this._CreatedAt = rentalAttr.CreatedAt;
110
- this._UpdatedById = rentalAttr.UpdatedById;
111
- this._UpdatedAt = rentalAttr.UpdatedAt;
112
- }
113
- }
114
-
115
- public static async init(dbTransaction?: any, rentalId?: string) {
116
- try {
117
- if (rentalId) {
118
- const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
119
- if (rental) {
120
- return new Rental(rental);
121
- } else {
122
- throw new ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
123
- }
124
- }
125
- return new Rental();
126
- } catch (error) {
127
- throw new ClassError(
128
- 'Rental',
129
- 'RentalErrMsg00',
130
- 'Failed To Initialize Price',
131
- );
132
- }
133
- }
134
-
135
- /**
136
- * Create a new Rental record.
137
- * @param {RentalPrice} rentalPrice - The rental pricing information.
138
- * @param {LoginUser} loginUser - The user initiating the creation.
139
- * @param {any} [dbTransaction] - Optional database transaction for atomic operations.
140
- * @throws {ClassError} Throws an error if:
141
- * 1. loginUser does not have 'Rental - Create' privilege.
142
- * 2. Rental item is not available at the current date.
143
- * @returns {Promise<any>} A Promise resolving to the created rental record.
144
- */
145
- public async create(
146
- rentalPrice: RentalPrice,
147
- loginUser: LoginUser,
148
- jointHirers?: JointHirer[],
149
- dbTransaction?: any,
150
- ): Promise<any> {
151
- try {
152
- /*Part 1: Check Privilege*/
153
- const systemCode =
154
- ApplicationConfig.getComponentConfigValue('system-code');
155
- const isPrivileged = await loginUser.checkPrivileges(
156
- systemCode,
157
- 'Rental - Create',
158
- );
159
-
160
- if (!isPrivileged) {
161
- throw new ClassError(
162
- 'Rental',
163
- 'RentalErrMsg01',
164
- "You do not have 'Rental - Create' privilege.",
165
- );
166
- }
167
-
168
- /*Part 2: Make Sure Rental Item Available*/
169
- const isItemAvailable = await Rental.isItemAvailable(
170
- this.ItemId,
171
- this.ItemType,
172
- this.StartDateTime,
173
- this.EndDateTime,
174
- dbTransaction,
175
- );
176
-
177
- if (!isItemAvailable) {
178
- throw new ClassError(
179
- 'Rental',
180
- 'RentalErrMsg02',
181
- 'Rental Item is not available at current date.',
182
- );
183
- }
184
-
185
- // Part 3: Create Rental Agreement Record
186
- // Call Rental._AgreementRepo create method by passing:
187
- // AgreementNo: this.AgreementNo
188
- // Status: "Not Generated"
189
- // dbTransaction
190
- await Rental._AgreementRepo.create(
191
- {
192
- AgreementNo: this.AgreementNo,
193
- Status: 'Not Generated',
194
- },
195
- {
196
- transaction: dbTransaction,
197
- },
198
- );
199
-
200
- //Create a record into rental_AgreementSignature table
201
- //Create the main customer's signature record
202
- await Rental._AgreementSignatureRepo.create(
203
- {
204
- SignatureId: this.createId(),
205
- AgreementNo: this.AgreementNo,
206
- Party: HirerTypeEnum.PRIMARY,
207
- PartyId: this.CustomerId,
208
- PartyType: 'Customer',
209
- SignatureStatus: AgreementSignatureStatusEnum.PENDING,
210
- SignedAt: new Date(),
211
- CreatedById: loginUser.ObjectId,
212
- CreatedAt: new Date(),
213
- UpdatedById: loginUser.ObjectId,
214
- UpdatedAt: new Date(),
215
- },
216
- { transaction: dbTransaction },
217
- );
218
-
219
- // Create signatures record for joint hirers if any
220
- if (jointHirers && jointHirers.length > 0) {
221
- for (const jointHirer of jointHirers) {
222
- await Rental._AgreementSignatureRepo.create(
223
- {
224
- SignatureId: this.createId(),
225
- AgreementNo: this.AgreementNo,
226
- Party: HirerTypeEnum.JOINT,
227
- PartyId: jointHirer.CustomerId,
228
- PartyType: 'Customer',
229
- SignatureStatus: AgreementSignatureStatusEnum.PENDING,
230
- SignedAt: new Date(),
231
- CreatedById: loginUser.ObjectId,
232
- CreatedAt: new Date(),
233
- UpdatedById: loginUser.ObjectId,
234
- UpdatedAt: new Date(),
235
- },
236
- { transaction: dbTransaction },
237
- );
238
- }
239
- }
240
-
241
- /*Part 4: Insert Rental & The Agreed Rental Price*/
242
- if (!rentalPrice.PriceId) {
243
- const rentalPriceData = await rentalPrice.create(
244
- loginUser,
245
- dbTransaction,
246
- );
247
- this.PriceId = rentalPriceData.PriceId;
248
- } else {
249
- this.PriceId = rentalPrice.PriceId;
250
- }
251
- this.RentalId = this.createId();
252
- // if (!this.Status) {
253
- this._Status = RentalStatusEnum.PENDING_SIGNING;
254
- // }
255
- this._CreatedById = loginUser.ObjectId;
256
- this._UpdatedById = loginUser.ObjectId;
257
- this._CreatedAt = new Date();
258
- this._UpdatedAt = new Date();
259
-
260
- const data = {
261
- RentalId: this.RentalId,
262
- CustomerId: this.CustomerId,
263
- CustomerType: this.CustomerType,
264
- ItemId: this.ItemId,
265
- ItemType: this.ItemType,
266
- PriceId: this.PriceId,
267
- StartDateTime: this.StartDateTime,
268
- EndDateTime: this.EndDateTime,
269
- CancelRemarks: this.CancelRemarks,
270
- TerminateRemarks: this.TerminateRemarks,
271
- AgreementNo: this.AgreementNo,
272
- AccountType: this.AccountType,
273
- Status: this.Status,
274
- EscheatmentYN: this.EscheatmentYN,
275
- CreatedById: this.CreatedById,
276
- CreatedAt: this.CreatedAt,
277
- UpdatedById: this.UpdatedById,
278
- UpdatedAt: this.UpdatedAt,
279
- };
280
-
281
- await Rental._Repo.create(data, {
282
- transaction: dbTransaction,
283
- });
284
-
285
- //For every data inside Param.jointHirers, update the rentalId and call jointHirer.create
286
- if (jointHirers) {
287
- for (let i = 0; i < jointHirers.length; i++) {
288
- jointHirers[i].RentalId = this.RentalId;
289
- await jointHirers[i].create(loginUser, dbTransaction);
290
- this.JointHirers.push(jointHirers[i]);
291
- }
292
- }
293
-
294
- /*Part 5: Record Create Rental Activity*/
295
- const activity = new Activity();
296
- activity.ActivityId = activity.createId();
297
- activity.Action = ActionEnum.CREATE;
298
- activity.Description = 'Add Rental';
299
- activity.EntityType = 'Rental';
300
- activity.EntityId = this.RentalId;
301
- activity.EntityValueBefore = JSON.stringify({});
302
- activity.EntityValueAfter = JSON.stringify(data);
303
- await activity.create(loginUser.ObjectId, dbTransaction);
304
-
305
- /*Part 6: Return Result*/
306
- return this;
307
- } catch (error) {
308
- throw error;
309
- }
310
- }
311
-
312
- public static async isItemAvailable(
313
- itemId: string,
314
- itemType: string,
315
- startDateTime: Date,
316
- endDateTime: Date,
317
- dbTransaction?: any,
318
- ) {
319
- try {
320
- const rental = await Rental._Repo.findOne({
321
- where: {
322
- ItemId: itemId,
323
- ItemType: itemType,
324
- StartDateTime: {
325
- [Op.lte]: endDateTime,
326
- },
327
- EndDateTime: {
328
- [Op.gte]: startDateTime,
329
- },
330
- },
331
- transaction: dbTransaction,
332
- });
333
-
334
- if (!rental) {
335
- // No rental found → item is available
336
- return true;
337
- }
338
-
339
- if (
340
- rental.Status === RentalStatusEnum.TERMINATED ||
341
- rental.Status === RentalStatusEnum.CANCELLED
342
- ) {
343
- // Rental found but status is Terminated or Cancelled → item is available
344
- return true;
345
- }
346
-
347
- // Rental exists and is active → item is not available
348
- return false;
349
- } catch (error) {
350
- throw error;
351
- }
352
- }
353
-
354
- public static async findAll(
355
- loginUser: LoginUser,
356
- dbTransaction: any,
357
- page?: number,
358
- row?: number,
359
- search?: IRentalFindAllSearchAttr,
360
- ) {
361
- try {
362
- const systemCode =
363
- await ApplicationConfig.getComponentConfigValue('system-code');
364
-
365
- const isPrivileged = await loginUser.checkPrivileges(
366
- systemCode,
367
- 'Rental - View',
368
- );
369
-
370
- if (!isPrivileged) {
371
- throw new ClassError(
372
- 'Rental',
373
- 'RentalErrMsg01',
374
- "You do not have 'Rental - View' privilege.",
375
- );
376
- }
377
-
378
- const queryObj: any = {};
379
-
380
- let options: any = {
381
- transaction: dbTransaction,
382
- order: [['CreatedAt', 'DESC']],
383
- };
384
-
385
- if (page && row) {
386
- options = {
387
- ...options,
388
- limit: row,
389
- offset: row * (page - 1),
390
- };
391
- }
392
-
393
- if (search) {
394
- Object.entries(search).forEach(([key, value]) => {
395
- if (key === 'StartDateTime') {
396
- queryObj[key] = {
397
- [Op.gte]: value,
398
- };
399
- } else if (key === 'EndDateTime') {
400
- queryObj[key] = {
401
- [Op.lte]: value,
402
- };
403
- } else if (key === 'CustomerId') {
404
- queryObj[key] = {
405
- [Op.substring]: value,
406
- };
407
-
408
- options.include = [
409
- {
410
- model: JointHirerModel,
411
- required: false,
412
- where: {
413
- CustomerId: {
414
- [Op.substring]: value,
415
- },
416
- },
417
- },
418
- ];
419
- } else {
420
- queryObj[key] = {
421
- [Op.substring]: value,
422
- };
423
- }
424
- });
425
- if (options?.include?.length > 1) {
426
- options.include.push({
427
- model: AgreementModel,
428
- required: false,
429
- });
430
- } else {
431
- options.include = [
432
- {
433
- model: AgreementModel,
434
- required: false,
435
- },
436
- ];
437
- }
438
-
439
- options = {
440
- ...options,
441
- where: queryObj,
442
- };
443
- }
444
-
445
- return await Rental._Repo.findAndCountAll(options);
446
- } catch (err) {
447
- throw err;
448
- }
449
- }
450
-
451
- public static async checkActiveByItemId(
452
- loginUser: LoginUser,
453
- itemId: string,
454
- itemType: string,
455
- dbTransaction?: any,
456
- ) {
457
- try {
458
- const systemCode =
459
- await ApplicationConfig.getComponentConfigValue('system-code');
460
-
461
- const isPrivileged = await loginUser.checkPrivileges(
462
- systemCode,
463
- 'Rental - View',
464
- );
465
-
466
- if (!isPrivileged) {
467
- throw new ClassError(
468
- 'Rental',
469
- 'RentalErrMsg01',
470
- "You do not have 'Rental - View' privilege.",
471
- );
472
- }
473
-
474
- const queryObj: any = {
475
- ItemId: itemId,
476
- ItemType: itemType,
477
- Status: RentalStatusEnum.ACTIVE,
478
- };
479
-
480
- const options: any = {
481
- transaction: dbTransaction,
482
- where: queryObj,
483
- };
484
-
485
- const rental = await Rental._Repo.findOne(options);
486
-
487
- if (rental) {
488
- return true;
489
- } else {
490
- return false;
491
- }
492
- } catch (err) {
493
- throw err;
494
- }
495
- }
496
-
497
- /**
498
- * Sets the StartDateTime property of the Rental class as custom setter.
499
- * @param {Date} startDateTime - The start date and time of the rental.
500
- * @throws {ClassError} Throws an error if:
501
- * 1. startDateTime is a past date.
502
- * 2. startDateTime is more than the EndDateTime.
503
- * @returns {void}
504
- */
505
- setStartDateTime(startDateTime: Date): void {
506
- const startDateTimeObject = new Date(startDateTime);
507
- startDateTimeObject.setHours(0, 0, 0, 0);
508
-
509
- /*Part 1: Make Sure Future Date Time*/
510
- if (startDateTimeObject < new Date(new Date().setHours(0, 0, 0, 0))) {
511
- throw new ClassError(
512
- 'Rental',
513
- 'RentalErrMsg02',
514
- 'StartDateTime cannot be a past datetime.',
515
- );
516
- }
517
-
518
- /*Part 2: Make Sure Less Than End Date Time*/
519
- if (this.EndDateTime && startDateTimeObject > this.EndDateTime) {
520
- throw new ClassError(
521
- 'Rental',
522
- 'RentalErrMsg03',
523
- 'StartDateTime cannot be more than EndDateTime.',
524
- );
525
- }
526
-
527
- /*Part 3: Set Status Whether Reserved or Active*/
528
- if (startDateTimeObject > new Date(new Date().setHours(0, 0, 0, 0))) {
529
- this._Status = RentalStatusEnum.RESERVED;
530
- } else {
531
- this._Status = RentalStatusEnum.ACTIVE;
532
- }
533
- }
534
-
535
- public async periodEndProcess(
536
- loginUser: LoginUser,
537
- dbTransaction: any,
538
- stockInventory: any,
539
- ) {
540
- try {
541
- // Privilege Checking
542
- const systemCode =
543
- await ApplicationConfig.getComponentConfigValue('system-code');
544
-
545
- const isPrivileged = await loginUser.checkPrivileges(
546
- systemCode,
547
- 'Rental PeriodEndProcess',
548
- );
549
-
550
- if (!isPrivileged) {
551
- throw new ClassError(
552
- 'Rental',
553
- 'RentalErrMsg04',
554
- "You do not have 'Rental - PeriodEndProcess' privilege.",
555
- );
556
- }
557
-
558
- // Validate Existing Rental
559
- if (this.AgreementNo === undefined || this.AgreementNo === null) {
560
- throw new ClassError(
561
- 'Rental',
562
- 'RentalErrMsg05',
563
- 'Rental must be existing rental.',
564
- );
565
- }
566
-
567
- // Validate Rental End Period
568
- if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
569
- throw new ClassError(
570
- 'Rental',
571
- 'RentalErrMsg06',
572
- 'Rental period is not ending today.',
573
- );
574
- }
575
-
576
- // Mark Rental Item as Available
577
- await stockInventory.setAvailable(loginUser, dbTransaction);
578
-
579
- // Capture Record Info Before Changes
580
- const entityValueBefore = {
581
- RentalId: this.RentalId,
582
- CustomerId: this.CustomerId,
583
- CustomerType: this.CustomerType,
584
- ItemId: this.ItemId,
585
- ItemType: this.ItemType,
586
- PriceId: this.PriceId,
587
- StartDateTime: this.StartDateTime,
588
- EndDateTime: this.EndDateTime,
589
- CancelRemarks: this.CancelRemarks,
590
- TerminateRemarks: this.TerminateRemarks,
591
- AgreementNo: this.AgreementNo,
592
- AccountType: this.AccountType,
593
- Status: this.Status,
594
- EscheatmentYN: this.EscheatmentYN,
595
- CreatedById: this.CreatedById,
596
- CreatedAt: this.CreatedAt,
597
- UpdatedById: this.UpdatedById,
598
- UpdatedAt: this.UpdatedAt,
599
- };
600
-
601
- // Mark Rental as Terminated and Capture Updater Info
602
- this.setTerminated();
603
- this._UpdatedById = loginUser.ObjectId;
604
- this._UpdatedAt = new Date();
605
-
606
- // Capture Record Info after Changes
607
- const entityValueAfter = {
608
- RentalId: this.RentalId,
609
- CustomerId: this.CustomerId,
610
- CustomerType: this.CustomerType,
611
- ItemId: this.ItemId,
612
- ItemType: this.ItemType,
613
- PriceId: this.PriceId,
614
- StartDateTime: this.StartDateTime,
615
- EndDateTime: this.EndDateTime,
616
- CancelRemarks: this.CancelRemarks,
617
- TerminateRemarks: this.TerminateRemarks,
618
- AgreementNo: this.AgreementNo,
619
- AccountType: this.AccountType,
620
- Status: this.Status,
621
- EscheatmentYN: this.EscheatmentYN,
622
- CreatedById: this.CreatedById,
623
- CreatedAt: this.CreatedAt,
624
- UpdatedById: this.UpdatedById,
625
- UpdatedAt: this.UpdatedAt,
626
- };
627
-
628
- // Record the Update Activity
629
- const activity = new Activity();
630
- activity.ActivityId = activity.createId();
631
- activity.Action = ActionEnum.UPDATE;
632
- activity.Description = 'Set Rental as Terminated';
633
- activity.EntityType = 'Rental';
634
- activity.EntityId = this.RentalId;
635
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
636
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
637
- await activity.create(loginUser.ObjectId, dbTransaction);
638
-
639
- return this;
640
- } catch (err) {
641
- throw err;
642
- }
643
- }
644
-
645
- async getCustomerActiveRentals(
646
- loginUser: LoginUser,
647
- dbTransaction: any,
648
- CustomerId: string,
649
- ): Promise<IRentalAttr[]> {
650
- try {
651
- // Part 1: Privilege Checking
652
- // Call loginUser.checkPrivileges() by passing:
653
- // SystemCode: <get_from_app_config>
654
- // PrivilegeCode: "RENTAL_VIEW"
655
- const systemCode =
656
- ApplicationConfig.getComponentConfigValue('system-code');
657
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
658
-
659
- if (!isPrivileged) {
660
- throw new ClassError(
661
- 'Rental',
662
- 'RentalErrMsg01',
663
- "You do not have 'Rental - View' privilege.",
664
- );
665
- }
666
-
667
- // Part 2: Retrieve Rentals and Returns
668
- // Call Rental._Repo findAll method by passing:
669
- // where:
670
- // Status: "Active"
671
- // [Op.OR]:
672
- // CustomerId: Params.CustomerId
673
- // '$JointHirers.CustomerId$': Params.CustomerId
674
- // include:
675
- // model: JointHirerModel
676
- // attributes: []
677
- const query = `
678
- SELECT
679
- r.*
680
- FROM
681
- rental_Rental r
682
- LEFT JOIN
683
- rental_JointHirer jh ON r.RentalId = jh.RentalId
684
- WHERE
685
- r.Status = 'Active'
686
- AND (
687
- r.CustomerId = '${CustomerId}'
688
- OR jh.CustomerId = '${CustomerId}'
689
- )
690
- GROUP BY
691
- r.RentalId
692
- `;
693
- const db = rentalDb.getConnection();
694
- const result = await db.query(query, {
695
- type: QueryTypes.SELECT,
696
- transaction: dbTransaction,
697
- model: RentalModel,
698
- mapToModel: true,
699
- });
700
-
701
- // Format the returned records
702
- const records: IRentalAttr[] = result.map((record: RentalModel) => {
703
- return {
704
- RentalId: record.RentalId,
705
- CustomerId: record.CustomerId,
706
- CustomerType: record.CustomerType,
707
- ItemId: record.ItemId,
708
- ItemType: record.ItemType,
709
- PriceId: record.PriceId,
710
- StartDateTime: record.StartDateTime,
711
- EndDateTime: record.EndDateTime,
712
- CancelRemarks: record.CancelRemarks,
713
- TerminateRemarks: record.TerminateRemarks,
714
- AgreementNo: record.AgreementNo,
715
- AccountType: record.AccountType,
716
- Status: record.Status,
717
- EscheatmentYN: record.EscheatmentYN,
718
- CreatedById: record.CreatedById,
719
- CreatedAt: record.CreatedAt,
720
- UpdatedById: record.UpdatedById,
721
- UpdatedAt: record.UpdatedAt,
722
- };
723
- });
724
- // Return the returned records.
725
- return records;
726
- } catch (error) {
727
- throw error;
728
- }
729
- }
730
-
731
- async getJointHirers(dbTransaction: any) {
732
- try {
733
- if (!this.RentalId) {
734
- throw new ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
735
- }
736
- if (this.AccountType !== RentalAccountTypeEnum.JOINT) {
737
- throw new ClassError(
738
- 'Rental',
739
- 'RentalErrMsg07',
740
- 'This rental does not have joint hirers.',
741
- );
742
- }
743
-
744
- const jointHirers = await Rental._JointHirerRepo.findAll({
745
- where: {
746
- RentalId: this.RentalId,
747
- },
748
- transaction: dbTransaction,
749
- });
750
- // Map the jointHirers to JointHirer instances
751
- const jointHirerInstances = Promise.all(
752
- jointHirers.map(async (hirer: JointHirerModel) => {
753
- return await JointHirer.init(hirer.HirerId, dbTransaction);
754
- }),
755
- );
756
- return jointHirerInstances;
757
- } catch (error) {
758
- console.error('Error in getJointHirers:', error);
759
- throw error;
760
- }
761
- }
762
-
763
- async getCustomerIds(loginUser: LoginUser, dbTransaction: any) {
764
- try {
765
- // Part 1: Privilege Checking
766
- // Call loginUser.checkPrivileges() by passing:
767
- // SystemCode: <get_from_app_config>
768
- // PrivilegeCode: "RENTAL_VIEW"
769
- const systemCode =
770
- ApplicationConfig.getComponentConfigValue('system-code');
771
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
772
-
773
- if (!isPrivileged) {
774
- throw new ClassError(
775
- 'Rental',
776
- 'RentalErrMsg01',
777
- "You do not have 'Rental - View' privilege.",
778
- );
779
- }
780
-
781
- // Part 2: Validation
782
- // Make sure this.RentalId exists, if not throw new ClassError by passing
783
- if (!this.RentalId) {
784
- throw new ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
785
- }
786
-
787
- //Part 3: Retrieve Listing and Returns
788
- // 3.1 Call Rental._JointHirerRepo findAll method by passing:
789
- // where:
790
- // RentalId: this.RentalId
791
- // dbTransaction
792
- const options: any = {
793
- where: {
794
- RentalId: this.RentalId,
795
- },
796
- transaction: dbTransaction,
797
- };
798
-
799
- const joinHirers = await Rental._JointHirerRepo.findAll(options);
800
-
801
- // 3.2 Create a new array variable and set its value to joint hirer and Rental.CustomerId
802
- // array translated to only CustomerId. Then, append this.CustomerId to the array.
803
- const customerIds: string[] = [this.CustomerId];
804
- for (let i = 0; i < joinHirers.length; i++) {
805
- const jointHirer = joinHirers[i];
806
- customerIds.push(jointHirer.CustomerId);
807
- }
808
-
809
- // 3.3 Return the array.
810
- return customerIds;
811
- } catch (error) {
812
- throw error;
813
- }
814
- }
815
-
816
- async signAgreement(
817
- loginUser: LoginUser,
818
- party: string,
819
- PartyId: string,
820
- PartyType: string,
821
- dbTransaction: any,
822
- statusAfterSign?: RentalStatusEnum | string,
823
- ) {
824
- try {
825
- // Part 1: Privilege Checking
826
- // Call loginUser.checkPrivileges() by passing:
827
- // SystemCode: <get_from_app_config>
828
- // PrivilegeCode: "RENTAL_SIGN"
829
- const systemCode =
830
- ApplicationConfig.getComponentConfigValue('system-code');
831
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_SIGN');
832
-
833
- if (!isPrivileged) {
834
- throw new ClassError(
835
- 'Rental',
836
- 'RentalErrMsg01',
837
- "You do not have 'RENTAL_SIGN' privilege.",
838
- );
839
- }
840
-
841
- // Part 2: Validation
842
- // Make sure this.Status is "Pending Signing" and agreementStatus is "Generated", if not throw new ClassError
843
- const agreement = await Agreement.init(this.AgreementNo, dbTransaction);
844
- if (
845
- this.Status !== RentalStatusEnum.PENDING_SIGNING &&
846
- agreement.Status !== AggrementStatusEnum.GENERATED
847
- ) {
848
- throw new ClassError(
849
- 'Rental',
850
- 'RentalErrMsg01',
851
- 'Rental is not ready to be signed yet.',
852
- );
853
- }
854
-
855
- //Make sure CustomerId inside the listing and SignatureStatus is "Pending"
856
- const signatureList = await Agreement.getSignatureList(
857
- loginUser,
858
- this.AgreementNo,
859
- dbTransaction,
860
- );
861
- const customerSignature = signatureList.find(
862
- (sig) =>
863
- sig.PartyId === PartyId &&
864
- sig.PartyType === PartyType &&
865
- sig.Party === party &&
866
- sig.SignatureStatus === AgreementSignatureStatusEnum.PENDING,
867
- );
868
-
869
- if (!customerSignature) {
870
- throw new ClassError(
871
- 'Rental',
872
- 'RentalErrMsg01',
873
- 'Signature is not pending.',
874
- );
875
- }
876
-
877
- //Update rental_AgreementSignature
878
- const signaturePayload = {
879
- SignatureStatus: AgreementSignatureStatusEnum.SIGNED,
880
- SignedAt: new Date(),
881
- UpdatedById: loginUser.ObjectId,
882
- UpdatedAt: new Date(),
883
- };
884
-
885
- await Rental._AgreementSignatureRepo.update(signaturePayload, {
886
- where: {
887
- AgreementNo: this.AgreementNo,
888
- Party: party,
889
- PartyId: PartyId,
890
- PartyType: PartyType,
891
- },
892
- transaction: dbTransaction,
893
- });
894
-
895
- const signatureActivity = new Activity();
896
- signatureActivity.ActivityId = signatureActivity.createId();
897
- signatureActivity.Action = ActionEnum.UPDATE;
898
- signatureActivity.Description = 'Update hirer signature';
899
- signatureActivity.EntityType = 'AgreementSignature';
900
- signatureActivity.EntityId = this.AgreementNo;
901
- signatureActivity.EntityValueBefore = JSON.stringify(customerSignature);
902
- signatureActivity.EntityValueAfter = JSON.stringify(signaturePayload);
903
- await signatureActivity.create(loginUser.ObjectId, dbTransaction);
904
-
905
- const updatedSignatureList = await Agreement.getSignatureList(
906
- loginUser,
907
- this.AgreementNo,
908
- dbTransaction,
909
- );
910
-
911
- const pendingSignatures = updatedSignatureList.filter(
912
- (sig) => sig.SignatureStatus === 'Pending',
913
- );
914
- if (pendingSignatures.length > 0) {
915
- return;
916
- }
917
-
918
- //Part 3: Update Agreement Status
919
- // 3.1 Set EntityValueBefore to current agreement instance.
920
- const entityValueAgreementBefore = {
921
- AgreementNo: agreement.AgreementNo,
922
- Status: agreement.Status,
923
- DateSigned: agreement.DateSigned,
924
- };
925
-
926
- // 3.2 Set below agreement attributes:
927
- // DateSigned: current date & time
928
- // Status: "Signed"
929
- const payload = {
930
- DateSigned: new Date(),
931
- Status: AggrementStatusEnum.SIGNED,
932
- };
933
-
934
- // 3.3 Call Rental._AgreementRepo update()
935
- await Rental._AgreementRepo.update(payload, {
936
- where: {
937
- AgreementNo: this.AgreementNo,
938
- },
939
- transaction: dbTransaction,
940
- });
941
-
942
- const entityValueAgreementAfter = {
943
- entityValueAgreementBefore,
944
- ...payload,
945
- };
946
-
947
- // Part 4: Record Update Agreement Activity
948
- const activity = new Activity();
949
- activity.ActivityId = activity.createId();
950
- activity.Action = ActionEnum.UPDATE;
951
- activity.Description = 'Update agreement';
952
- activity.EntityType = 'RentalAgreement';
953
- activity.EntityId = this.AgreementNo;
954
- activity.EntityValueBefore = JSON.stringify(entityValueAgreementBefore);
955
- activity.EntityValueAfter = JSON.stringify(entityValueAgreementAfter);
956
- await activity.create(loginUser.ObjectId, dbTransaction);
957
-
958
- // Part 5: Update Rental Status
959
- // 5.1 Set EntityValueBefore to current rental instance.
960
- const entityValueRentalBefore = {
961
- RentalId: this.RentalId,
962
- CustomerId: this.CustomerId,
963
- CustomerType: this.CustomerType,
964
- ItemId: this.ItemId,
965
- ItemType: this.ItemType,
966
- PriceId: this.PriceId,
967
- StartDateTime: this.StartDateTime,
968
- EndDateTime: this.EndDateTime,
969
- CancelRemarks: this.CancelRemarks,
970
- TerminateRemarks: this.TerminateRemarks,
971
- AgreementNo: this.AgreementNo,
972
- AccountType: this.AccountType,
973
- Status: this.Status,
974
- EscheatmentYN: this.EscheatmentYN,
975
- CreatedById: this.CreatedById,
976
- CreatedAt: this.CreatedAt,
977
- UpdatedById: this.UpdatedById,
978
- UpdatedAt: this.UpdatedAt,
979
- };
980
- // 5.2: Set below rental attributes:
981
- // Status: "Pending Key Collection"
982
- // UpdatedById: loginUser.ObjectId
983
- // UpdatedAt: current date & time
984
-
985
- this._Status = statusAfterSign
986
- ? statusAfterSign
987
- : RentalStatusEnum.ACTIVE;
988
-
989
- const payloadRental = {
990
- Status: this._Status,
991
- UpdatedById: loginUser.ObjectId,
992
- UpdatedAt: new Date(),
993
- };
994
-
995
- // 5.3: Call Rental._Repo update() method by passing:
996
- // populate rental instance attributes
997
- // dbTransaction
998
- await Rental._Repo.update(payloadRental, {
999
- where: {
1000
- RentalId: this.RentalId,
1001
- },
1002
- transaction: dbTransaction,
1003
- });
1004
-
1005
- const entityValueRentalAfter = {
1006
- entityValueRentalBefore,
1007
- ...payloadRental,
1008
- };
1009
-
1010
- // Part 6: Record Update Agreement Activity
1011
- const rentalActivity = new Activity();
1012
- rentalActivity.ActivityId = activity.createId();
1013
- rentalActivity.Action = ActionEnum.UPDATE;
1014
- rentalActivity.Description = 'Sign rental agreement';
1015
- rentalActivity.EntityType = 'Rental';
1016
- rentalActivity.EntityId = this.RentalId;
1017
- rentalActivity.EntityValueBefore = JSON.stringify(
1018
- entityValueRentalBefore,
1019
- );
1020
- rentalActivity.EntityValueAfter = JSON.stringify(entityValueRentalAfter);
1021
- await rentalActivity.create(loginUser.ObjectId, dbTransaction);
1022
- } catch (error) {
1023
- throw error;
1024
- }
1025
- }
1026
-
1027
- async generateAgreement(
1028
- loginUser: LoginUser,
1029
- dbTransaction: any,
1030
- MediaId: string,
1031
- ) {
1032
- //This method will generate a new rental agreement.
1033
- try {
1034
- // Part 1: Privilege Checking
1035
- // Call loginUser.checkPrivileges() by passing:
1036
- // SystemCode: "<get_from_app_config>"
1037
- // PrivilegeCode: "RENTAL_AGREEMENT_CREATE"
1038
-
1039
- const systemCode =
1040
- ApplicationConfig.getComponentConfigValue('system-code');
1041
- const isPrivileged = loginUser.checkPrivileges(
1042
- systemCode,
1043
- 'RENTAL_AGREEMENT_CREATE',
1044
- );
1045
-
1046
- if (!isPrivileged) {
1047
- throw new ClassError(
1048
- 'Rental',
1049
- 'RentalErrMsg01',
1050
- "You do not have 'RENTAL_AGREEMENT_CREATE' privilege.",
1051
- );
1052
- }
1053
-
1054
- // Part 2: Validation
1055
- // Instantiate existing RentalAgreement by passing:
1056
- // dbTransaction
1057
- // AgreementNo: this.AgreementNo
1058
- // Make sure Params.MediaId exists, if not throw new ClassError by passing:
1059
- // Classname: "Rental"
1060
- // MessageCode: "RentalErrMsg0X"
1061
- // Message: "MediaId is required."
1062
- const agreement = await Agreement.init(this.AgreementNo, dbTransaction);
1063
- if (!MediaId) {
1064
- throw new ClassError(
1065
- 'Rental',
1066
- 'RentalErrMsg0X',
1067
- 'MediaId is required.',
1068
- );
1069
- }
1070
-
1071
- // Part 3: Update Agreement Record
1072
- // Set EntityValueBefore to the agreement instance.
1073
- // Set below agreement attributes:
1074
- // Status: "Generated"
1075
- // MediaId: Params.MediaId
1076
-
1077
- const EntityValueBefore = agreement;
1078
- agreement.Status = AggrementStatusEnum.GENERATED;
1079
- agreement.MediaId = MediaId;
1080
-
1081
- // Call Rental._AgreementRepo update method by passing:
1082
- // Status: agreement.Status
1083
- // MediaId: agreement.MediaId
1084
- // dbTransaction
1085
-
1086
- await Rental._AgreementRepo.update(
1087
- {
1088
- Status: agreement.Status,
1089
- MediaId: agreement.MediaId,
1090
- },
1091
- {
1092
- where: {
1093
- AgreementNo: this.AgreementNo,
1094
- },
1095
- transaction: dbTransaction,
1096
- },
1097
- );
1098
-
1099
- // Part 4: Record Update Agreement Activity
1100
- // Initialise EntityValueAfter variable and set to agreement instance
1101
- const EntityValueAfter = agreement;
1102
- // Instantiate new activity from Activity class, call createId() method, then set:
1103
- const activity = new Activity();
1104
- // Action: ActionEnum.UPDATE
1105
- // Description: "Generate agreement."
1106
- // EntityType: "RentalAgreement"
1107
- // EntityId: this.AgreementNo
1108
- // EntityValueBefore: EntityValueBefore
1109
- // EntityValueAfter: EntityValueAfter
1110
- activity.ActivityId = activity.createId();
1111
- activity.Action = ActionEnum.UPDATE;
1112
- activity.Description = 'Generate agreement.';
1113
- activity.EntityType = 'RentalAgreement';
1114
- activity.EntityId = this.AgreementNo;
1115
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
1116
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
1117
-
1118
- // Call new activity create method by passing:
1119
- // dbTransaction
1120
- // userId: loginUser.ObjectId
1121
- await activity.create(loginUser.ObjectId, dbTransaction);
1122
-
1123
- // Part 5: Add Record To Agreement History Table
1124
- // Make sure there is a private static readonly _AgreementHistoryRepo variable.
1125
- // Set newRecord to an object with below key-value:
1126
- // AgreementNo: this.AgreementNo
1127
- // MediaId: params.MediaId
1128
- // ActivityCompleted: "Agreement Generated"
1129
- // CreatedAt: Current Timestamp
1130
- // Use _AgreementHistoryRepo.create() method and pass newRecord and dbTransaction.
1131
- const agreementHistory = await Rental._AgreementHistoryRepo.create(
1132
- {
1133
- AgreementNo: this.AgreementNo,
1134
- MediaId,
1135
- ActivityCompleted: 'Agreement Generated',
1136
- CreatedAt: new Date(),
1137
- },
1138
- { transaction: dbTransaction },
1139
- );
1140
-
1141
- // Part 6: Record Agreement History Activity
1142
- // Initialise EntityValueAfter variable and set to newRecord from previous part.
1143
- // Instantiate new activity from Activity class, call createId() method, then set:
1144
- // Action: ActionEnum.CREATE
1145
- // Description: "Generate agreement."
1146
- // EntityType: "RentalAgreementHistory"
1147
- // EntityId: HistoryId from previous part.
1148
- // EntityValueBefore: empty object.
1149
- // EntityValueAfter: EntityValueAfter
1150
- // Call new activity create method by passing:
1151
- // dbTransaction
1152
- // userId: loginUser.ObjectId
1153
- const entityValueAfter = agreementHistory.get({ plain: true });
1154
- const activityHistory = new Activity();
1155
- activityHistory.ActivityId = activityHistory.createId();
1156
- activityHistory.Action = ActionEnum.CREATE;
1157
- activityHistory.Description = 'Generate agreement.';
1158
- activityHistory.EntityType = 'RentalAgreementHistory';
1159
- activityHistory.EntityId = agreementHistory.HistoryId.toString();
1160
- activityHistory.EntityValueBefore = JSON.stringify({});
1161
- activityHistory.EntityValueAfter = JSON.stringify(entityValueAfter);
1162
- await activityHistory.create(loginUser.ObjectId, dbTransaction);
1163
- } catch (error) {
1164
- throw error;
1165
- }
1166
- }
1167
- }
1
+ import { IRentalAttr } from '../../interfaces/rental-attr.interface';
2
+ import { RentalStatusEnum } from '../../enum/rental-status.enum';
3
+ import { RentalRepository } from './rental.repository';
4
+ import { ClassError, ObjectBase } from '@tomei/general';
5
+ import { ApplicationConfig } from '@tomei/config';
6
+ import { AuthContext, LoginUser, System } from '@tomei/sso';
7
+ import { RentalPrice } from '../rental-price/rental-price';
8
+ import { Op, QueryTypes } from 'sequelize';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
10
+ import { IRentalFindAllSearchAttr } from '../../interfaces/rental-find-all-search-attr.interface';
11
+ import { RentalAccountTypeEnum } from '../../enum/account-type.enum';
12
+ import { JointHirer } from '../joint-hirer/joint-hirer';
13
+ import { JointHirerModel } from '../../models/joint-hirer.entity';
14
+ import { AgreementRepository } from '../agreement/agreement.repository';
15
+ import * as rentalDb from '../../database';
16
+ import { RentalModel } from '../../models/rental.entity';
17
+ import { JointHirerRepository } from '../joint-hirer/joint-hirer.repository';
18
+ import { AgreementModel } from '../../models';
19
+ import { Agreement } from '../agreement/agreement';
20
+ import { AggrementStatusEnum } from '../../enum/aggrement-status.enum';
21
+ import { AgreementSignatureRepository } from '../agreement-signature/agreement-signature.repository';
22
+ import { AgreementSignatureStatusEnum } from '../../enum/agreement-signature-status.enum';
23
+ import { HirerTypeEnum } from '../../enum/hirer-type.enum';
24
+ import { AgreementHistoryRepository } from '../agreement-history/agreement-history.repository';
25
+ import { DocType, Document, IAccountSystem } from '@tomei/finance';
26
+ import { ItemClassMap } from '../../ClassMappings/ItemClassMap';
27
+
28
+ export class Rental extends ObjectBase {
29
+ ObjectId: string;
30
+ ObjectName: string;
31
+ ObjectType = 'Rental';
32
+ TableName: string;
33
+ CustomerId: string;
34
+ CustomerType: string;
35
+ ItemId: string;
36
+ ItemType: string;
37
+ PriceId: string;
38
+ StartDateTime: Date;
39
+ EndDateTime: Date;
40
+ CancelRemarks: string;
41
+ TerminateRemarks: string;
42
+ AgreementNo: string;
43
+ AccountType: RentalAccountTypeEnum;
44
+ JointHirers: JointHirer[] = [];
45
+ Invoices: Document[] = [];
46
+ Item: any;
47
+ protected _Status: RentalStatusEnum | string;
48
+ protected _EscheatmentYN: string = 'N';
49
+ protected _CreatedById: string;
50
+ protected _CreatedAt: Date;
51
+ protected _UpdatedById: string;
52
+ protected _UpdatedAt: Date;
53
+ protected static _Repo = new RentalRepository();
54
+ protected static _AgreementRepo = new AgreementRepository();
55
+ protected static _JointHirerRepo = new JointHirerRepository();
56
+ protected static _AgreementSignatureRepo = new AgreementSignatureRepository();
57
+ protected static _AgreementHistoryRepo = new AgreementHistoryRepository();
58
+
59
+ get RentalId(): string {
60
+ return this.ObjectId;
61
+ }
62
+
63
+ set RentalId(value: string) {
64
+ this.ObjectId = value;
65
+ }
66
+
67
+ get Status(): RentalStatusEnum | string {
68
+ return this._Status;
69
+ }
70
+
71
+ get EscheatmentYN(): string {
72
+ return this._EscheatmentYN;
73
+ }
74
+
75
+ get CreatedById(): string {
76
+ return this._CreatedById;
77
+ }
78
+
79
+ get CreatedAt(): Date {
80
+ return this._CreatedAt;
81
+ }
82
+
83
+ get UpdatedById(): string {
84
+ return this._UpdatedById;
85
+ }
86
+
87
+ get UpdatedAt(): Date {
88
+ return this._UpdatedAt;
89
+ }
90
+
91
+ private setTerminated() {
92
+ this._Status = RentalStatusEnum.TERMINATED;
93
+ }
94
+
95
+ protected constructor(rentalAttr?: IRentalAttr) {
96
+ super();
97
+ if (rentalAttr) {
98
+ this.RentalId = rentalAttr.RentalId;
99
+ this.CustomerId = rentalAttr.CustomerId;
100
+ this.CustomerType = rentalAttr.CustomerType;
101
+ this.ItemId = rentalAttr.ItemId;
102
+ this.ItemType = rentalAttr.ItemType;
103
+ this.PriceId = rentalAttr.PriceId;
104
+ this.StartDateTime = rentalAttr.StartDateTime;
105
+ this.EndDateTime = rentalAttr.EndDateTime;
106
+ this.CancelRemarks = rentalAttr.CancelRemarks;
107
+ this.TerminateRemarks = rentalAttr.TerminateRemarks;
108
+ this.AgreementNo = rentalAttr.AgreementNo;
109
+ this.AccountType = rentalAttr.AccountType;
110
+ this._Status = rentalAttr.Status;
111
+ this._EscheatmentYN = rentalAttr.EscheatmentYN;
112
+ this._CreatedById = rentalAttr.CreatedById;
113
+ this._CreatedAt = rentalAttr.CreatedAt;
114
+ this._UpdatedById = rentalAttr.UpdatedById;
115
+ this._UpdatedAt = rentalAttr.UpdatedAt;
116
+ }
117
+ }
118
+
119
+ public static async init(dbTransaction?: any, rentalId?: string) {
120
+ try {
121
+ if (rentalId) {
122
+ const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
123
+ if (rental) {
124
+ return new Rental(rental);
125
+ } else {
126
+ throw new ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
127
+ }
128
+ }
129
+ return new Rental();
130
+ } catch (error) {
131
+ throw new ClassError(
132
+ 'Rental',
133
+ 'RentalErrMsg00',
134
+ 'Failed To Initialize Price',
135
+ );
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Create a new Rental record.
141
+ * @param {RentalPrice} rentalPrice - The rental pricing information.
142
+ * @param {LoginUser} loginUser - The user initiating the creation.
143
+ * @param {any} [dbTransaction] - Optional database transaction for atomic operations.
144
+ * @throws {ClassError} Throws an error if:
145
+ * 1. loginUser does not have 'Rental - Create' privilege.
146
+ * 2. Rental item is not available at the current date.
147
+ * @returns {Promise<any>} A Promise resolving to the created rental record.
148
+ */
149
+ public async create(
150
+ rentalPrice: RentalPrice,
151
+ loginUser: LoginUser,
152
+ jointHirers?: JointHirer[],
153
+ dbTransaction?: any,
154
+ ): Promise<any> {
155
+ try {
156
+ /*Part 1: Check Privilege*/
157
+ const systemCode =
158
+ ApplicationConfig.getComponentConfigValue('system-code');
159
+ const isPrivileged = await loginUser.checkPrivileges(
160
+ systemCode,
161
+ 'Rental - Create',
162
+ );
163
+
164
+ if (!isPrivileged) {
165
+ throw new ClassError(
166
+ 'Rental',
167
+ 'RentalErrMsg01',
168
+ "You do not have 'Rental - Create' privilege.",
169
+ );
170
+ }
171
+
172
+ /*Part 2: Make Sure Rental Item Available*/
173
+ const isItemAvailable = await Rental.isItemAvailable(
174
+ this.ItemId,
175
+ this.ItemType,
176
+ this.StartDateTime,
177
+ this.EndDateTime,
178
+ dbTransaction,
179
+ );
180
+
181
+ if (!isItemAvailable) {
182
+ throw new ClassError(
183
+ 'Rental',
184
+ 'RentalErrMsg02',
185
+ 'Rental Item is not available at current date.',
186
+ );
187
+ }
188
+
189
+ // Part 3: Create Rental Agreement Record
190
+ // Call Rental._AgreementRepo create method by passing:
191
+ // AgreementNo: this.AgreementNo
192
+ // Status: "Not Generated"
193
+ // dbTransaction
194
+ await Rental._AgreementRepo.create(
195
+ {
196
+ AgreementNo: this.AgreementNo,
197
+ Status: 'Not Generated',
198
+ },
199
+ {
200
+ transaction: dbTransaction,
201
+ },
202
+ );
203
+
204
+ //Create a record into rental_AgreementSignature table
205
+ //Create the main customer's signature record
206
+ await Rental._AgreementSignatureRepo.create(
207
+ {
208
+ SignatureId: this.createId(),
209
+ AgreementNo: this.AgreementNo,
210
+ Party: HirerTypeEnum.PRIMARY,
211
+ PartyId: this.CustomerId,
212
+ PartyType: 'Customer',
213
+ SignatureStatus: AgreementSignatureStatusEnum.PENDING,
214
+ SignedAt: new Date(),
215
+ CreatedById: loginUser.ObjectId,
216
+ CreatedAt: new Date(),
217
+ UpdatedById: loginUser.ObjectId,
218
+ UpdatedAt: new Date(),
219
+ },
220
+ { transaction: dbTransaction },
221
+ );
222
+
223
+ // Create signatures record for joint hirers if any
224
+ if (jointHirers && jointHirers.length > 0) {
225
+ for (const jointHirer of jointHirers) {
226
+ await Rental._AgreementSignatureRepo.create(
227
+ {
228
+ SignatureId: this.createId(),
229
+ AgreementNo: this.AgreementNo,
230
+ Party: HirerTypeEnum.JOINT,
231
+ PartyId: jointHirer.CustomerId,
232
+ PartyType: 'Customer',
233
+ SignatureStatus: AgreementSignatureStatusEnum.PENDING,
234
+ SignedAt: new Date(),
235
+ CreatedById: loginUser.ObjectId,
236
+ CreatedAt: new Date(),
237
+ UpdatedById: loginUser.ObjectId,
238
+ UpdatedAt: new Date(),
239
+ },
240
+ { transaction: dbTransaction },
241
+ );
242
+ }
243
+ }
244
+
245
+ /*Part 4: Insert Rental & The Agreed Rental Price*/
246
+ if (!rentalPrice.PriceId) {
247
+ const rentalPriceData = await rentalPrice.create(
248
+ loginUser,
249
+ dbTransaction,
250
+ );
251
+ this.PriceId = rentalPriceData.PriceId;
252
+ } else {
253
+ this.PriceId = rentalPrice.PriceId;
254
+ }
255
+ this.RentalId = this.createId();
256
+ // if (!this.Status) {
257
+ this._Status = RentalStatusEnum.PENDING_SIGNING;
258
+ // }
259
+ this._CreatedById = loginUser.ObjectId;
260
+ this._UpdatedById = loginUser.ObjectId;
261
+ this._CreatedAt = new Date();
262
+ this._UpdatedAt = new Date();
263
+
264
+ const data = {
265
+ RentalId: this.RentalId,
266
+ CustomerId: this.CustomerId,
267
+ CustomerType: this.CustomerType,
268
+ ItemId: this.ItemId,
269
+ ItemType: this.ItemType,
270
+ PriceId: this.PriceId,
271
+ StartDateTime: this.StartDateTime,
272
+ EndDateTime: this.EndDateTime,
273
+ CancelRemarks: this.CancelRemarks,
274
+ TerminateRemarks: this.TerminateRemarks,
275
+ AgreementNo: this.AgreementNo,
276
+ AccountType: this.AccountType,
277
+ Status: this.Status,
278
+ EscheatmentYN: this.EscheatmentYN,
279
+ CreatedById: this.CreatedById,
280
+ CreatedAt: this.CreatedAt,
281
+ UpdatedById: this.UpdatedById,
282
+ UpdatedAt: this.UpdatedAt,
283
+ };
284
+
285
+ await Rental._Repo.create(data, {
286
+ transaction: dbTransaction,
287
+ });
288
+
289
+ //For every data inside Param.jointHirers, update the rentalId and call jointHirer.create
290
+ if (jointHirers) {
291
+ for (let i = 0; i < jointHirers.length; i++) {
292
+ jointHirers[i].RentalId = this.RentalId;
293
+ await jointHirers[i].create(loginUser, dbTransaction);
294
+ this.JointHirers.push(jointHirers[i]);
295
+ }
296
+ }
297
+
298
+ /*Part 5: Record Create Rental Activity*/
299
+ const activity = new Activity();
300
+ activity.ActivityId = activity.createId();
301
+ activity.Action = ActionEnum.CREATE;
302
+ activity.Description = 'Add Rental';
303
+ activity.EntityType = 'Rental';
304
+ activity.EntityId = this.RentalId;
305
+ activity.EntityValueBefore = JSON.stringify({});
306
+ activity.EntityValueAfter = JSON.stringify(data);
307
+ await activity.create(loginUser.ObjectId, dbTransaction);
308
+
309
+ /*Part 6: Return Result*/
310
+ return this;
311
+ } catch (error) {
312
+ throw error;
313
+ }
314
+ }
315
+
316
+ public static async isItemAvailable(
317
+ itemId: string,
318
+ itemType: string,
319
+ startDateTime: Date,
320
+ endDateTime: Date,
321
+ dbTransaction?: any,
322
+ ) {
323
+ try {
324
+ const rental = await Rental._Repo.findOne({
325
+ where: {
326
+ ItemId: itemId,
327
+ ItemType: itemType,
328
+ StartDateTime: {
329
+ [Op.lte]: endDateTime,
330
+ },
331
+ EndDateTime: {
332
+ [Op.gte]: startDateTime,
333
+ },
334
+ },
335
+ transaction: dbTransaction,
336
+ });
337
+
338
+ if (!rental) {
339
+ // No rental found → item is available
340
+ return true;
341
+ }
342
+
343
+ if (
344
+ rental.Status === RentalStatusEnum.TERMINATED ||
345
+ rental.Status === RentalStatusEnum.CANCELLED
346
+ ) {
347
+ // Rental found but status is Terminated or Cancelled → item is available
348
+ return true;
349
+ }
350
+
351
+ // Rental exists and is active → item is not available
352
+ return false;
353
+ } catch (error) {
354
+ throw error;
355
+ }
356
+ }
357
+
358
+ public static async findAll(
359
+ authContext: AuthContext,
360
+ dbTransaction: any,
361
+ page?: number,
362
+ row?: number,
363
+ search?: IRentalFindAllSearchAttr,
364
+ ) {
365
+ try {
366
+ const systemCode =
367
+ await ApplicationConfig.getComponentConfigValue('system-code');
368
+
369
+ if ('loginUser' in authContext) {
370
+ const isPrivileged = await authContext.loginUser.checkPrivileges(
371
+ systemCode,
372
+ 'Rental - View',
373
+ );
374
+
375
+ if (!isPrivileged) {
376
+ throw new ClassError(
377
+ 'Rental',
378
+ 'RentalErrMsg01',
379
+ "You do not have 'Rental - View' privilege.",
380
+ );
381
+ }
382
+ }
383
+
384
+ const queryObj: any = {};
385
+
386
+ let options: any = {
387
+ transaction: dbTransaction,
388
+ order: [['CreatedAt', 'DESC']],
389
+ };
390
+
391
+ if (page && row) {
392
+ options = {
393
+ ...options,
394
+ limit: row,
395
+ offset: row * (page - 1),
396
+ };
397
+ }
398
+
399
+ if (search) {
400
+ Object.entries(search).forEach(([key, value]) => {
401
+ if (key === 'StartDateTime') {
402
+ queryObj[key] = {
403
+ [Op.gte]: value,
404
+ };
405
+ } else if (key === 'EndDateTime') {
406
+ queryObj[key] = {
407
+ [Op.lte]: value,
408
+ };
409
+ } else if (key === 'CustomerId') {
410
+ queryObj[key] = {
411
+ [Op.substring]: value,
412
+ };
413
+
414
+ options.include = [
415
+ {
416
+ model: JointHirerModel,
417
+ required: false,
418
+ where: {
419
+ CustomerId: {
420
+ [Op.substring]: value,
421
+ },
422
+ },
423
+ },
424
+ ];
425
+ } else {
426
+ queryObj[key] = {
427
+ [Op.substring]: value,
428
+ };
429
+ }
430
+ });
431
+ if (options?.include?.length > 1) {
432
+ options.include.push({
433
+ model: AgreementModel,
434
+ required: false,
435
+ });
436
+ } else {
437
+ options.include = [
438
+ {
439
+ model: AgreementModel,
440
+ required: false,
441
+ },
442
+ ];
443
+ }
444
+
445
+ options = {
446
+ ...options,
447
+ where: queryObj,
448
+ };
449
+ }
450
+
451
+ return await Rental._Repo.findAndCountAll(options);
452
+ } catch (err) {
453
+ throw err;
454
+ }
455
+ }
456
+
457
+ public static async checkActiveByItemId(
458
+ loginUser: LoginUser,
459
+ itemId: string,
460
+ itemType: string,
461
+ dbTransaction?: any,
462
+ ) {
463
+ try {
464
+ const systemCode =
465
+ await ApplicationConfig.getComponentConfigValue('system-code');
466
+
467
+ const isPrivileged = await loginUser.checkPrivileges(
468
+ systemCode,
469
+ 'Rental - View',
470
+ );
471
+
472
+ if (!isPrivileged) {
473
+ throw new ClassError(
474
+ 'Rental',
475
+ 'RentalErrMsg01',
476
+ "You do not have 'Rental - View' privilege.",
477
+ );
478
+ }
479
+
480
+ const queryObj: any = {
481
+ ItemId: itemId,
482
+ ItemType: itemType,
483
+ Status: RentalStatusEnum.ACTIVE,
484
+ };
485
+
486
+ const options: any = {
487
+ transaction: dbTransaction,
488
+ where: queryObj,
489
+ };
490
+
491
+ const rental = await Rental._Repo.findOne(options);
492
+
493
+ if (rental) {
494
+ return true;
495
+ } else {
496
+ return false;
497
+ }
498
+ } catch (err) {
499
+ throw err;
500
+ }
501
+ }
502
+
503
+ /**
504
+ * Sets the StartDateTime property of the Rental class as custom setter.
505
+ * @param {Date} startDateTime - The start date and time of the rental.
506
+ * @throws {ClassError} Throws an error if:
507
+ * 1. startDateTime is a past date.
508
+ * 2. startDateTime is more than the EndDateTime.
509
+ * @returns {void}
510
+ */
511
+ setStartDateTime(startDateTime: Date): void {
512
+ const startDateTimeObject = new Date(startDateTime);
513
+ startDateTimeObject.setHours(0, 0, 0, 0);
514
+
515
+ /*Part 1: Make Sure Future Date Time*/
516
+ if (startDateTimeObject < new Date(new Date().setHours(0, 0, 0, 0))) {
517
+ throw new ClassError(
518
+ 'Rental',
519
+ 'RentalErrMsg02',
520
+ 'StartDateTime cannot be a past datetime.',
521
+ );
522
+ }
523
+
524
+ /*Part 2: Make Sure Less Than End Date Time*/
525
+ if (this.EndDateTime && startDateTimeObject > this.EndDateTime) {
526
+ throw new ClassError(
527
+ 'Rental',
528
+ 'RentalErrMsg03',
529
+ 'StartDateTime cannot be more than EndDateTime.',
530
+ );
531
+ }
532
+
533
+ /*Part 3: Set Status Whether Reserved or Active*/
534
+ if (startDateTimeObject > new Date(new Date().setHours(0, 0, 0, 0))) {
535
+ this._Status = RentalStatusEnum.RESERVED;
536
+ } else {
537
+ this._Status = RentalStatusEnum.ACTIVE;
538
+ }
539
+ }
540
+
541
+ public async periodEndProcess(
542
+ loginUser: LoginUser,
543
+ dbTransaction: any,
544
+ stockInventory: any,
545
+ ) {
546
+ try {
547
+ // Privilege Checking
548
+ const systemCode =
549
+ await ApplicationConfig.getComponentConfigValue('system-code');
550
+
551
+ const isPrivileged = await loginUser.checkPrivileges(
552
+ systemCode,
553
+ 'Rental – PeriodEndProcess',
554
+ );
555
+
556
+ if (!isPrivileged) {
557
+ throw new ClassError(
558
+ 'Rental',
559
+ 'RentalErrMsg04',
560
+ "You do not have 'Rental - PeriodEndProcess' privilege.",
561
+ );
562
+ }
563
+
564
+ // Validate Existing Rental
565
+ if (this.AgreementNo === undefined || this.AgreementNo === null) {
566
+ throw new ClassError(
567
+ 'Rental',
568
+ 'RentalErrMsg05',
569
+ 'Rental must be existing rental.',
570
+ );
571
+ }
572
+
573
+ // Validate Rental End Period
574
+ if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
575
+ throw new ClassError(
576
+ 'Rental',
577
+ 'RentalErrMsg06',
578
+ 'Rental period is not ending today.',
579
+ );
580
+ }
581
+
582
+ // Mark Rental Item as Available
583
+ await stockInventory.setAvailable(loginUser, dbTransaction);
584
+
585
+ // Capture Record Info Before Changes
586
+ const entityValueBefore = {
587
+ RentalId: this.RentalId,
588
+ CustomerId: this.CustomerId,
589
+ CustomerType: this.CustomerType,
590
+ ItemId: this.ItemId,
591
+ ItemType: this.ItemType,
592
+ PriceId: this.PriceId,
593
+ StartDateTime: this.StartDateTime,
594
+ EndDateTime: this.EndDateTime,
595
+ CancelRemarks: this.CancelRemarks,
596
+ TerminateRemarks: this.TerminateRemarks,
597
+ AgreementNo: this.AgreementNo,
598
+ AccountType: this.AccountType,
599
+ Status: this.Status,
600
+ EscheatmentYN: this.EscheatmentYN,
601
+ CreatedById: this.CreatedById,
602
+ CreatedAt: this.CreatedAt,
603
+ UpdatedById: this.UpdatedById,
604
+ UpdatedAt: this.UpdatedAt,
605
+ };
606
+
607
+ // Mark Rental as Terminated and Capture Updater Info
608
+ this.setTerminated();
609
+ this._UpdatedById = loginUser.ObjectId;
610
+ this._UpdatedAt = new Date();
611
+
612
+ // Capture Record Info after Changes
613
+ const entityValueAfter = {
614
+ RentalId: this.RentalId,
615
+ CustomerId: this.CustomerId,
616
+ CustomerType: this.CustomerType,
617
+ ItemId: this.ItemId,
618
+ ItemType: this.ItemType,
619
+ PriceId: this.PriceId,
620
+ StartDateTime: this.StartDateTime,
621
+ EndDateTime: this.EndDateTime,
622
+ CancelRemarks: this.CancelRemarks,
623
+ TerminateRemarks: this.TerminateRemarks,
624
+ AgreementNo: this.AgreementNo,
625
+ AccountType: this.AccountType,
626
+ Status: this.Status,
627
+ EscheatmentYN: this.EscheatmentYN,
628
+ CreatedById: this.CreatedById,
629
+ CreatedAt: this.CreatedAt,
630
+ UpdatedById: this.UpdatedById,
631
+ UpdatedAt: this.UpdatedAt,
632
+ };
633
+
634
+ // Record the Update Activity
635
+ const activity = new Activity();
636
+ activity.ActivityId = activity.createId();
637
+ activity.Action = ActionEnum.UPDATE;
638
+ activity.Description = 'Set Rental as Terminated';
639
+ activity.EntityType = 'Rental';
640
+ activity.EntityId = this.RentalId;
641
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
642
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
643
+ await activity.create(loginUser.ObjectId, dbTransaction);
644
+
645
+ return this;
646
+ } catch (err) {
647
+ throw err;
648
+ }
649
+ }
650
+
651
+ async getCustomerActiveRentals(
652
+ loginUser: LoginUser,
653
+ dbTransaction: any,
654
+ CustomerId: string,
655
+ ): Promise<IRentalAttr[]> {
656
+ try {
657
+ // Part 1: Privilege Checking
658
+ // Call loginUser.checkPrivileges() by passing:
659
+ // SystemCode: <get_from_app_config>
660
+ // PrivilegeCode: "RENTAL_VIEW"
661
+ const systemCode =
662
+ ApplicationConfig.getComponentConfigValue('system-code');
663
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
664
+
665
+ if (!isPrivileged) {
666
+ throw new ClassError(
667
+ 'Rental',
668
+ 'RentalErrMsg01',
669
+ "You do not have 'Rental - View' privilege.",
670
+ );
671
+ }
672
+
673
+ // Part 2: Retrieve Rentals and Returns
674
+ // Call Rental._Repo findAll method by passing:
675
+ // where:
676
+ // Status: "Active"
677
+ // [Op.OR]:
678
+ // CustomerId: Params.CustomerId
679
+ // '$JointHirers.CustomerId$': Params.CustomerId
680
+ // include:
681
+ // model: JointHirerModel
682
+ // attributes: []
683
+ const query = `
684
+ SELECT
685
+ r.*
686
+ FROM
687
+ rental_Rental r
688
+ LEFT JOIN
689
+ rental_JointHirer jh ON r.RentalId = jh.RentalId
690
+ WHERE
691
+ r.Status = 'Active'
692
+ AND (
693
+ r.CustomerId = '${CustomerId}'
694
+ OR jh.CustomerId = '${CustomerId}'
695
+ )
696
+ GROUP BY
697
+ r.RentalId
698
+ `;
699
+ const db = rentalDb.getConnection();
700
+ const result = await db.query(query, {
701
+ type: QueryTypes.SELECT,
702
+ transaction: dbTransaction,
703
+ model: RentalModel,
704
+ mapToModel: true,
705
+ });
706
+
707
+ // Format the returned records
708
+ const records: IRentalAttr[] = result.map((record: RentalModel) => {
709
+ return {
710
+ RentalId: record.RentalId,
711
+ CustomerId: record.CustomerId,
712
+ CustomerType: record.CustomerType,
713
+ ItemId: record.ItemId,
714
+ ItemType: record.ItemType,
715
+ PriceId: record.PriceId,
716
+ StartDateTime: record.StartDateTime,
717
+ EndDateTime: record.EndDateTime,
718
+ CancelRemarks: record.CancelRemarks,
719
+ TerminateRemarks: record.TerminateRemarks,
720
+ AgreementNo: record.AgreementNo,
721
+ AccountType: record.AccountType,
722
+ Status: record.Status,
723
+ EscheatmentYN: record.EscheatmentYN,
724
+ CreatedById: record.CreatedById,
725
+ CreatedAt: record.CreatedAt,
726
+ UpdatedById: record.UpdatedById,
727
+ UpdatedAt: record.UpdatedAt,
728
+ };
729
+ });
730
+ // Return the returned records.
731
+ return records;
732
+ } catch (error) {
733
+ throw error;
734
+ }
735
+ }
736
+
737
+ async getJointHirers(dbTransaction: any) {
738
+ try {
739
+ if (!this.RentalId) {
740
+ throw new ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
741
+ }
742
+ if (this.AccountType !== RentalAccountTypeEnum.JOINT) {
743
+ throw new ClassError(
744
+ 'Rental',
745
+ 'RentalErrMsg07',
746
+ 'This rental does not have joint hirers.',
747
+ );
748
+ }
749
+
750
+ const jointHirers = await Rental._JointHirerRepo.findAll({
751
+ where: {
752
+ RentalId: this.RentalId,
753
+ },
754
+ transaction: dbTransaction,
755
+ });
756
+ // Map the jointHirers to JointHirer instances
757
+ const jointHirerInstances = Promise.all(
758
+ jointHirers.map(async (hirer: JointHirerModel) => {
759
+ return await JointHirer.init(hirer.HirerId, dbTransaction);
760
+ }),
761
+ );
762
+ return jointHirerInstances;
763
+ } catch (error) {
764
+ console.error('Error in getJointHirers:', error);
765
+ throw error;
766
+ }
767
+ }
768
+
769
+ async getCustomerIds(loginUser: LoginUser, dbTransaction: any) {
770
+ try {
771
+ // Part 1: Privilege Checking
772
+ // Call loginUser.checkPrivileges() by passing:
773
+ // SystemCode: <get_from_app_config>
774
+ // PrivilegeCode: "RENTAL_VIEW"
775
+ const systemCode =
776
+ ApplicationConfig.getComponentConfigValue('system-code');
777
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
778
+
779
+ if (!isPrivileged) {
780
+ throw new ClassError(
781
+ 'Rental',
782
+ 'RentalErrMsg01',
783
+ "You do not have 'Rental - View' privilege.",
784
+ );
785
+ }
786
+
787
+ // Part 2: Validation
788
+ // Make sure this.RentalId exists, if not throw new ClassError by passing
789
+ if (!this.RentalId) {
790
+ throw new ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
791
+ }
792
+
793
+ //Part 3: Retrieve Listing and Returns
794
+ // 3.1 Call Rental._JointHirerRepo findAll method by passing:
795
+ // where:
796
+ // RentalId: this.RentalId
797
+ // dbTransaction
798
+ const options: any = {
799
+ where: {
800
+ RentalId: this.RentalId,
801
+ },
802
+ transaction: dbTransaction,
803
+ };
804
+
805
+ const joinHirers = await Rental._JointHirerRepo.findAll(options);
806
+
807
+ // 3.2 Create a new array variable and set its value to joint hirer and Rental.CustomerId
808
+ // array translated to only CustomerId. Then, append this.CustomerId to the array.
809
+ const customerIds: string[] = [this.CustomerId];
810
+ for (let i = 0; i < joinHirers.length; i++) {
811
+ const jointHirer = joinHirers[i];
812
+ customerIds.push(jointHirer.CustomerId);
813
+ }
814
+
815
+ // 3.3 Return the array.
816
+ return customerIds;
817
+ } catch (error) {
818
+ throw error;
819
+ }
820
+ }
821
+
822
+ async signAgreement(
823
+ loginUser: LoginUser,
824
+ party: string,
825
+ PartyId: string,
826
+ PartyType: string,
827
+ dbTransaction: any,
828
+ statusAfterSign?: RentalStatusEnum | string,
829
+ ) {
830
+ try {
831
+ // Part 1: Privilege Checking
832
+ // Call loginUser.checkPrivileges() by passing:
833
+ // SystemCode: <get_from_app_config>
834
+ // PrivilegeCode: "RENTAL_SIGN"
835
+ const systemCode =
836
+ ApplicationConfig.getComponentConfigValue('system-code');
837
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_SIGN');
838
+
839
+ if (!isPrivileged) {
840
+ throw new ClassError(
841
+ 'Rental',
842
+ 'RentalErrMsg01',
843
+ "You do not have 'RENTAL_SIGN' privilege.",
844
+ );
845
+ }
846
+
847
+ // Part 2: Validation
848
+ // Make sure this.Status is "Pending Signing" and agreementStatus is "Generated", if not throw new ClassError
849
+ const agreement = await Agreement.init(this.AgreementNo, dbTransaction);
850
+ if (
851
+ this.Status !== RentalStatusEnum.PENDING_SIGNING &&
852
+ agreement.Status !== AggrementStatusEnum.GENERATED
853
+ ) {
854
+ throw new ClassError(
855
+ 'Rental',
856
+ 'RentalErrMsg01',
857
+ 'Rental is not ready to be signed yet.',
858
+ );
859
+ }
860
+
861
+ //Make sure CustomerId inside the listing and SignatureStatus is "Pending"
862
+ const signatureList = await Agreement.getSignatureList(
863
+ loginUser,
864
+ this.AgreementNo,
865
+ dbTransaction,
866
+ );
867
+ const customerSignature = signatureList.find(
868
+ (sig) =>
869
+ sig.PartyId === PartyId &&
870
+ sig.PartyType === PartyType &&
871
+ sig.Party === party &&
872
+ sig.SignatureStatus === AgreementSignatureStatusEnum.PENDING,
873
+ );
874
+
875
+ if (!customerSignature) {
876
+ throw new ClassError(
877
+ 'Rental',
878
+ 'RentalErrMsg01',
879
+ 'Signature is not pending.',
880
+ );
881
+ }
882
+
883
+ //Update rental_AgreementSignature
884
+ const signaturePayload = {
885
+ SignatureStatus: AgreementSignatureStatusEnum.SIGNED,
886
+ SignedAt: new Date(),
887
+ UpdatedById: loginUser.ObjectId,
888
+ UpdatedAt: new Date(),
889
+ };
890
+
891
+ await Rental._AgreementSignatureRepo.update(signaturePayload, {
892
+ where: {
893
+ AgreementNo: this.AgreementNo,
894
+ Party: party,
895
+ PartyId: PartyId,
896
+ PartyType: PartyType,
897
+ },
898
+ transaction: dbTransaction,
899
+ });
900
+
901
+ const signatureActivity = new Activity();
902
+ signatureActivity.ActivityId = signatureActivity.createId();
903
+ signatureActivity.Action = ActionEnum.UPDATE;
904
+ signatureActivity.Description = 'Update hirer signature';
905
+ signatureActivity.EntityType = 'AgreementSignature';
906
+ signatureActivity.EntityId = this.AgreementNo;
907
+ signatureActivity.EntityValueBefore = JSON.stringify(customerSignature);
908
+ signatureActivity.EntityValueAfter = JSON.stringify(signaturePayload);
909
+ await signatureActivity.create(loginUser.ObjectId, dbTransaction);
910
+
911
+ const updatedSignatureList = await Agreement.getSignatureList(
912
+ loginUser,
913
+ this.AgreementNo,
914
+ dbTransaction,
915
+ );
916
+
917
+ const pendingSignatures = updatedSignatureList.filter(
918
+ (sig) => sig.SignatureStatus === 'Pending',
919
+ );
920
+ if (pendingSignatures.length > 0) {
921
+ return;
922
+ }
923
+
924
+ //Part 3: Update Agreement Status
925
+ // 3.1 Set EntityValueBefore to current agreement instance.
926
+ const entityValueAgreementBefore = {
927
+ AgreementNo: agreement.AgreementNo,
928
+ Status: agreement.Status,
929
+ DateSigned: agreement.DateSigned,
930
+ };
931
+
932
+ // 3.2 Set below agreement attributes:
933
+ // DateSigned: current date & time
934
+ // Status: "Signed"
935
+ const payload = {
936
+ DateSigned: new Date(),
937
+ Status: AggrementStatusEnum.SIGNED,
938
+ };
939
+
940
+ // 3.3 Call Rental._AgreementRepo update()
941
+ await Rental._AgreementRepo.update(payload, {
942
+ where: {
943
+ AgreementNo: this.AgreementNo,
944
+ },
945
+ transaction: dbTransaction,
946
+ });
947
+
948
+ const entityValueAgreementAfter = {
949
+ entityValueAgreementBefore,
950
+ ...payload,
951
+ };
952
+
953
+ // Part 4: Record Update Agreement Activity
954
+ const activity = new Activity();
955
+ activity.ActivityId = activity.createId();
956
+ activity.Action = ActionEnum.UPDATE;
957
+ activity.Description = 'Update agreement';
958
+ activity.EntityType = 'RentalAgreement';
959
+ activity.EntityId = this.AgreementNo;
960
+ activity.EntityValueBefore = JSON.stringify(entityValueAgreementBefore);
961
+ activity.EntityValueAfter = JSON.stringify(entityValueAgreementAfter);
962
+ await activity.create(loginUser.ObjectId, dbTransaction);
963
+
964
+ // Part 5: Update Rental Status
965
+ // 5.1 Set EntityValueBefore to current rental instance.
966
+ const entityValueRentalBefore = {
967
+ RentalId: this.RentalId,
968
+ CustomerId: this.CustomerId,
969
+ CustomerType: this.CustomerType,
970
+ ItemId: this.ItemId,
971
+ ItemType: this.ItemType,
972
+ PriceId: this.PriceId,
973
+ StartDateTime: this.StartDateTime,
974
+ EndDateTime: this.EndDateTime,
975
+ CancelRemarks: this.CancelRemarks,
976
+ TerminateRemarks: this.TerminateRemarks,
977
+ AgreementNo: this.AgreementNo,
978
+ AccountType: this.AccountType,
979
+ Status: this.Status,
980
+ EscheatmentYN: this.EscheatmentYN,
981
+ CreatedById: this.CreatedById,
982
+ CreatedAt: this.CreatedAt,
983
+ UpdatedById: this.UpdatedById,
984
+ UpdatedAt: this.UpdatedAt,
985
+ };
986
+ // 5.2: Set below rental attributes:
987
+ // Status: "Pending Key Collection"
988
+ // UpdatedById: loginUser.ObjectId
989
+ // UpdatedAt: current date & time
990
+
991
+ this._Status = statusAfterSign
992
+ ? statusAfterSign
993
+ : RentalStatusEnum.ACTIVE;
994
+
995
+ const payloadRental = {
996
+ Status: this._Status,
997
+ UpdatedById: loginUser.ObjectId,
998
+ UpdatedAt: new Date(),
999
+ };
1000
+
1001
+ // 5.3: Call Rental._Repo update() method by passing:
1002
+ // populate rental instance attributes
1003
+ // dbTransaction
1004
+ await Rental._Repo.update(payloadRental, {
1005
+ where: {
1006
+ RentalId: this.RentalId,
1007
+ },
1008
+ transaction: dbTransaction,
1009
+ });
1010
+
1011
+ const entityValueRentalAfter = {
1012
+ entityValueRentalBefore,
1013
+ ...payloadRental,
1014
+ };
1015
+
1016
+ // Part 6: Record Update Agreement Activity
1017
+ const rentalActivity = new Activity();
1018
+ rentalActivity.ActivityId = activity.createId();
1019
+ rentalActivity.Action = ActionEnum.UPDATE;
1020
+ rentalActivity.Description = 'Sign rental agreement';
1021
+ rentalActivity.EntityType = 'Rental';
1022
+ rentalActivity.EntityId = this.RentalId;
1023
+ rentalActivity.EntityValueBefore = JSON.stringify(
1024
+ entityValueRentalBefore,
1025
+ );
1026
+ rentalActivity.EntityValueAfter = JSON.stringify(entityValueRentalAfter);
1027
+ await rentalActivity.create(loginUser.ObjectId, dbTransaction);
1028
+ } catch (error) {
1029
+ throw error;
1030
+ }
1031
+ }
1032
+
1033
+ async generateAgreement(
1034
+ loginUser: LoginUser,
1035
+ dbTransaction: any,
1036
+ MediaId: string,
1037
+ ) {
1038
+ //This method will generate a new rental agreement.
1039
+ try {
1040
+ // Part 1: Privilege Checking
1041
+ // Call loginUser.checkPrivileges() by passing:
1042
+ // SystemCode: "<get_from_app_config>"
1043
+ // PrivilegeCode: "RENTAL_AGREEMENT_CREATE"
1044
+
1045
+ const systemCode =
1046
+ ApplicationConfig.getComponentConfigValue('system-code');
1047
+ const isPrivileged = loginUser.checkPrivileges(
1048
+ systemCode,
1049
+ 'RENTAL_AGREEMENT_CREATE',
1050
+ );
1051
+
1052
+ if (!isPrivileged) {
1053
+ throw new ClassError(
1054
+ 'Rental',
1055
+ 'RentalErrMsg01',
1056
+ "You do not have 'RENTAL_AGREEMENT_CREATE' privilege.",
1057
+ );
1058
+ }
1059
+
1060
+ // Part 2: Validation
1061
+ // Instantiate existing RentalAgreement by passing:
1062
+ // dbTransaction
1063
+ // AgreementNo: this.AgreementNo
1064
+ // Make sure Params.MediaId exists, if not throw new ClassError by passing:
1065
+ // Classname: "Rental"
1066
+ // MessageCode: "RentalErrMsg0X"
1067
+ // Message: "MediaId is required."
1068
+ const agreement = await Agreement.init(this.AgreementNo, dbTransaction);
1069
+ if (!MediaId) {
1070
+ throw new ClassError(
1071
+ 'Rental',
1072
+ 'RentalErrMsg0X',
1073
+ 'MediaId is required.',
1074
+ );
1075
+ }
1076
+
1077
+ // Part 3: Update Agreement Record
1078
+ // Set EntityValueBefore to the agreement instance.
1079
+ // Set below agreement attributes:
1080
+ // Status: "Generated"
1081
+ // MediaId: Params.MediaId
1082
+
1083
+ const EntityValueBefore = agreement;
1084
+ agreement.Status = AggrementStatusEnum.GENERATED;
1085
+ agreement.MediaId = MediaId;
1086
+
1087
+ // Call Rental._AgreementRepo update method by passing:
1088
+ // Status: agreement.Status
1089
+ // MediaId: agreement.MediaId
1090
+ // dbTransaction
1091
+
1092
+ await Rental._AgreementRepo.update(
1093
+ {
1094
+ Status: agreement.Status,
1095
+ MediaId: agreement.MediaId,
1096
+ },
1097
+ {
1098
+ where: {
1099
+ AgreementNo: this.AgreementNo,
1100
+ },
1101
+ transaction: dbTransaction,
1102
+ },
1103
+ );
1104
+
1105
+ // Part 4: Record Update Agreement Activity
1106
+ // Initialise EntityValueAfter variable and set to agreement instance
1107
+ const EntityValueAfter = agreement;
1108
+ // Instantiate new activity from Activity class, call createId() method, then set:
1109
+ const activity = new Activity();
1110
+ // Action: ActionEnum.UPDATE
1111
+ // Description: "Generate agreement."
1112
+ // EntityType: "RentalAgreement"
1113
+ // EntityId: this.AgreementNo
1114
+ // EntityValueBefore: EntityValueBefore
1115
+ // EntityValueAfter: EntityValueAfter
1116
+ activity.ActivityId = activity.createId();
1117
+ activity.Action = ActionEnum.UPDATE;
1118
+ activity.Description = 'Generate agreement.';
1119
+ activity.EntityType = 'RentalAgreement';
1120
+ activity.EntityId = this.AgreementNo;
1121
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
1122
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
1123
+
1124
+ // Call new activity create method by passing:
1125
+ // dbTransaction
1126
+ // userId: loginUser.ObjectId
1127
+ await activity.create(loginUser.ObjectId, dbTransaction);
1128
+
1129
+ // Part 5: Add Record To Agreement History Table
1130
+ // Make sure there is a private static readonly _AgreementHistoryRepo variable.
1131
+ // Set newRecord to an object with below key-value:
1132
+ // AgreementNo: this.AgreementNo
1133
+ // MediaId: params.MediaId
1134
+ // ActivityCompleted: "Agreement Generated"
1135
+ // CreatedAt: Current Timestamp
1136
+ // Use _AgreementHistoryRepo.create() method and pass newRecord and dbTransaction.
1137
+ const agreementHistory = await Rental._AgreementHistoryRepo.create(
1138
+ {
1139
+ AgreementNo: this.AgreementNo,
1140
+ MediaId,
1141
+ ActivityCompleted: 'Agreement Generated',
1142
+ CreatedAt: new Date(),
1143
+ },
1144
+ { transaction: dbTransaction },
1145
+ );
1146
+
1147
+ // Part 6: Record Agreement History Activity
1148
+ // Initialise EntityValueAfter variable and set to newRecord from previous part.
1149
+ // Instantiate new activity from Activity class, call createId() method, then set:
1150
+ // Action: ActionEnum.CREATE
1151
+ // Description: "Generate agreement."
1152
+ // EntityType: "RentalAgreementHistory"
1153
+ // EntityId: HistoryId from previous part.
1154
+ // EntityValueBefore: empty object.
1155
+ // EntityValueAfter: EntityValueAfter
1156
+ // Call new activity create method by passing:
1157
+ // dbTransaction
1158
+ // userId: loginUser.ObjectId
1159
+ const entityValueAfter = agreementHistory.get({ plain: true });
1160
+ const activityHistory = new Activity();
1161
+ activityHistory.ActivityId = activityHistory.createId();
1162
+ activityHistory.Action = ActionEnum.CREATE;
1163
+ activityHistory.Description = 'Generate agreement.';
1164
+ activityHistory.EntityType = 'RentalAgreementHistory';
1165
+ activityHistory.EntityId = agreementHistory.HistoryId.toString();
1166
+ activityHistory.EntityValueBefore = JSON.stringify({});
1167
+ activityHistory.EntityValueAfter = JSON.stringify(entityValueAfter);
1168
+ await activityHistory.create(loginUser.ObjectId, dbTransaction);
1169
+ } catch (error) {
1170
+ throw error;
1171
+ }
1172
+ }
1173
+
1174
+ public toJSON(): IRentalAttr {
1175
+ return {
1176
+ RentalId: this.RentalId,
1177
+ CustomerId: this.CustomerId,
1178
+ CustomerType: this.CustomerType,
1179
+ ItemId: this.ItemId,
1180
+ ItemType: this.ItemType,
1181
+ PriceId: this.PriceId,
1182
+ StartDateTime: this.StartDateTime,
1183
+ EndDateTime: this.EndDateTime,
1184
+ Status: this.Status,
1185
+ CancelRemarks: this.CancelRemarks,
1186
+ TerminateRemarks: this.TerminateRemarks,
1187
+ EscheatmentYN: this.EscheatmentYN,
1188
+ AgreementNo: this.AgreementNo,
1189
+ AccountType: this.AccountType,
1190
+ CreatedById: this.CreatedById,
1191
+ CreatedAt: this.CreatedAt,
1192
+ UpdatedById: this.UpdatedById,
1193
+ UpdatedAt: this.UpdatedAt,
1194
+ };
1195
+ }
1196
+
1197
+ public async getInvoices(
1198
+ loginUser: LoginUser,
1199
+ dbTransaction: any,
1200
+ accountingSystem?: IAccountSystem,
1201
+ ) {
1202
+ try {
1203
+ // Part 1: Privilege Checking
1204
+ // Call loginUser.checkPrivileges() by passing:
1205
+ // SystemCode: <get_from_app_config>
1206
+ // PrivilegeCode: "RENTAL_VIEW_INVOICE"
1207
+ const systemCode =
1208
+ ApplicationConfig.getComponentConfigValue('system-code');
1209
+ const isPrivileged = loginUser.checkPrivileges(
1210
+ systemCode,
1211
+ 'RENTAL_VIEW_INVOICE',
1212
+ );
1213
+
1214
+ if (!isPrivileged) {
1215
+ throw new ClassError(
1216
+ 'Rental',
1217
+ 'RentalErrMsg01',
1218
+ "You do not have 'RENTAL_VIEW_INVOICE' privilege.",
1219
+ );
1220
+ }
1221
+
1222
+ // Part 2: Validation
1223
+ // Make sure this._ObjectId exists, if not throw new ClassError by passing
1224
+ if (!this.ObjectId) {
1225
+ throw new ClassError(
1226
+ 'Rental',
1227
+ 'RentalErrMsg01',
1228
+ 'ObjectId is missing.',
1229
+ );
1230
+ }
1231
+
1232
+ // Part 3: Check Existing Invoices
1233
+ if (this.Invoices.length > 0) {
1234
+ return this.Invoices;
1235
+ }
1236
+
1237
+ // Part 4: Retrieve Invoices and Returns
1238
+ const search = {
1239
+ DocType: DocType.INVOICE,
1240
+ RelatedObjectId: this.ObjectId,
1241
+ RelatedObjectType: this.ObjectType,
1242
+ };
1243
+
1244
+ const documents = await Document.findAll(
1245
+ loginUser,
1246
+ dbTransaction,
1247
+ undefined,
1248
+ undefined,
1249
+ search,
1250
+ undefined,
1251
+ accountingSystem,
1252
+ );
1253
+
1254
+ this.Invoices = documents.rows.map((doc) => {
1255
+ const document = new Document(dbTransaction, doc);
1256
+ return document;
1257
+ });
1258
+
1259
+ return this.Invoices;
1260
+ } catch (error) {
1261
+ throw error;
1262
+ }
1263
+ }
1264
+
1265
+ public async getItem(dbTransaction: any): Promise<any> {
1266
+ if (this.Item) {
1267
+ return this.Item;
1268
+ } else {
1269
+ const ItemClass = ItemClassMap[this.ItemType];
1270
+
1271
+ if (!ItemClass || typeof ItemClass.init !== 'function') {
1272
+ throw new Error(`Invalid or unregistered ItemType: ${this.ItemType}`);
1273
+ }
1274
+
1275
+ ItemClass._Repo = ItemClass._Repo || ItemClass.getRepo();
1276
+
1277
+ if (!ItemClass._Repo) {
1278
+ throw new Error(
1279
+ `ItemType ${this.ItemType} does not have a repository.`,
1280
+ );
1281
+ }
1282
+
1283
+ const itemInstance = await ItemClass.init(this.ItemId, dbTransaction);
1284
+
1285
+ if (!itemInstance) {
1286
+ throw new Error(`${this.ItemType} not found with ID ${this.ItemId}`);
1287
+ }
1288
+ this.Item = itemInstance;
1289
+
1290
+ // Make sure item has setAvailable method
1291
+ if (typeof this.Item.setAvailable !== 'function') {
1292
+ throw new Error(`${this.ItemType} does not implement setAvailable()`);
1293
+ }
1294
+ return this.Item;
1295
+ }
1296
+ }
1297
+
1298
+ public async cancel(
1299
+ loginUser: LoginUser,
1300
+ dbTransaction: any,
1301
+ remarks: string,
1302
+ ): Promise<Rental> {
1303
+ try {
1304
+ // Part 1: Privilege Checking
1305
+ const systemCode =
1306
+ ApplicationConfig.getComponentConfigValue('system-code');
1307
+ const isPrivileged = await loginUser.checkPrivileges(
1308
+ systemCode,
1309
+ 'RENTAL_CANCEL',
1310
+ );
1311
+
1312
+ if (!isPrivileged) {
1313
+ throw new ClassError(
1314
+ 'Rental',
1315
+ 'RentalErrMsg01',
1316
+ "You do not have 'RENTAL_CANCEL' privilege.",
1317
+ );
1318
+ }
1319
+
1320
+ // Part 2: Validation
1321
+ if (!this.ObjectId) {
1322
+ throw new ClassError(
1323
+ 'Rental',
1324
+ 'RentalErrMsg01',
1325
+ 'Rental not found. Rental Id is empty.',
1326
+ );
1327
+ }
1328
+
1329
+ if (
1330
+ this.Status === RentalStatusEnum.ACTIVE ||
1331
+ this.Status === RentalStatusEnum.SUSPENDED
1332
+ ) {
1333
+ throw new ClassError(
1334
+ 'Rental',
1335
+ 'RentalErrMsg01',
1336
+ `Rental already ${this.Status}, please do termination process.`,
1337
+ );
1338
+ }
1339
+
1340
+ if (!remarks) {
1341
+ throw new ClassError(
1342
+ 'Rental',
1343
+ 'RentalErrMsg01',
1344
+ 'Cancellation remarks is required.',
1345
+ );
1346
+ }
1347
+
1348
+ // Part 3: Capture Record Info Before Changes
1349
+ const entityValueBefore = this.toJSON();
1350
+
1351
+ // Part 4: Update Rental Status and Remarks
1352
+ this._Status = RentalStatusEnum.CANCELLED;
1353
+ this.CancelRemarks = remarks;
1354
+ this._UpdatedById = loginUser.ObjectId;
1355
+ this._UpdatedAt = new Date();
1356
+
1357
+ // Part 5: Update Rental Record
1358
+ await Rental._Repo.update(
1359
+ {
1360
+ Status: this.Status,
1361
+ CancelRemarks: this.CancelRemarks,
1362
+ UpdatedById: this.UpdatedById,
1363
+ UpdatedAt: this.UpdatedAt,
1364
+ },
1365
+ {
1366
+ where: {
1367
+ RentalId: this.RentalId,
1368
+ },
1369
+ transaction: dbTransaction,
1370
+ },
1371
+ );
1372
+
1373
+ // Part 6: Mark Item as Available
1374
+ const item = await this.getItem(dbTransaction);
1375
+
1376
+ await item.setAvailable(loginUser, dbTransaction);
1377
+
1378
+ // Part 7: Capture Record Info After Changes
1379
+ const entityValueAfter = this.toJSON();
1380
+ // Part 8: Record Activity
1381
+ const activity = new Activity();
1382
+ activity.ActivityId = activity.createId();
1383
+ activity.Action = ActionEnum.UPDATE;
1384
+ activity.Description = 'Rental Cancellation';
1385
+ activity.EntityType = this.ObjectType;
1386
+ activity.EntityId = this.ObjectId;
1387
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1388
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
1389
+ await activity.create(loginUser.ObjectId, dbTransaction);
1390
+ return this;
1391
+ } catch (error) {
1392
+ throw error;
1393
+ }
1394
+ }
1395
+ }