patch-biz-real.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import fs from 'fs'
  2. const path = new URL('../src/api/biz.ts', import.meta.url)
  3. let s = fs.readFileSync(path, 'utf8')
  4. if (!s.includes("from './sjnmtybt/realBiz'")) {
  5. s = s.replace(
  6. "import {\n mockAccounts,",
  7. "import * as real from './sjnmtybt/realBiz'\nimport {\n mockAccounts,",
  8. )
  9. }
  10. const pairs = [
  11. ['fetchDashboard', 'params'],
  12. ['fetchVillageBatches', ''],
  13. ['fetchPeople', 'params'],
  14. ['savePerson', 'payload'],
  15. ['submitVillageBatch', 'batchId'],
  16. ['fetchInsuranceBatches', ''],
  17. ['fetchInsuranceResults', 'params'],
  18. ['triggerInsuranceMatch', 'batchIds'],
  19. ['confirmInsuranceManual', 'personId'],
  20. ['submitToLeader', 'batchId'],
  21. ['exportCompareList', '_batchId'],
  22. ['returnBatchToTown', 'batchId, reason'],
  23. ['fetchApprovals', 'params'],
  24. ['reviewPersonQualify', 'payload'],
  25. ['decideApproval', 'payload'],
  26. ['fetchStatusBatches', ''],
  27. ['fetchStandards', 'town'],
  28. ['createStandard', 'payload'],
  29. ['updateStandard', 'payload'],
  30. ['updateDeathStatus', 'deathId'],
  31. ['applyPersonStatusAction', 'payload'],
  32. ['fetchSpecials', ''],
  33. ['fetchDeaths', ''],
  34. ['createDeathRecord', 'payload'],
  35. ['createSpecialBiz', 'payload'],
  36. ['exportSpecialSummary', 'type'],
  37. ['triggerAdjustReissue', 'town'],
  38. ['fetchAdjustReissueRoster', 'params'],
  39. ['fetchVillageMonthlyBatches', 'town'],
  40. ['fetchMonthlyBatches', ''],
  41. ['calculateMonthly', ''],
  42. ['confirmMonthly', 'id'],
  43. ['exportMonthlyFile', 'id'],
  44. ['issueVillageMonthlyDisk', 'batchId'],
  45. ['confirmVillageMonthlyReturn', 'payload'],
  46. ['fetchDisks', 'town'],
  47. ['exportDisk', ''],
  48. ['confirmReturnDisk', 'payload'],
  49. ['uploadReturnDiskFile', 'payload'],
  50. ['fetchDiskFailPeople', 'diskId'],
  51. ['confirmDiskFailPaid', 'failPersonId'],
  52. ['archiveToNextMonth', 'id'],
  53. ['fetchAlerts', ''],
  54. ['handleAlert', 'payload, result'],
  55. ['fetchReportBatches', ''],
  56. ['fetchAccounts', ''],
  57. ['fetchOpLogs', ''],
  58. ]
  59. const lines = s.split('\n')
  60. const out = []
  61. let i = 0
  62. let patched = 0
  63. while (i < lines.length) {
  64. const line = lines[i]
  65. const m = line.match(/^export function (\w+)/)
  66. if (!m) {
  67. out.push(line)
  68. i++
  69. continue
  70. }
  71. const fname = m[1]
  72. const pair = pairs.find((p) => p[0] === fname)
  73. let j = i
  74. let brace = 0
  75. let started = false
  76. const block = []
  77. while (j < lines.length) {
  78. const L = lines[j]
  79. block.push(L)
  80. for (const ch of L) {
  81. if (ch === '{') {
  82. brace++
  83. started = true
  84. }
  85. if (ch === '}') brace--
  86. }
  87. j++
  88. if (started && brace === 0) break
  89. }
  90. if (!pair) {
  91. out.push(...block)
  92. i = j
  93. continue
  94. }
  95. let body = block.join('\n')
  96. const args = pair[1]
  97. const call = args ? `return real.${fname}(${args})` : `return real.${fname}()`
  98. // Replace the final real-api return (get/post) after mock branch
  99. const next = body.replace(/\n return (?:get|post)[\s\S]*$/m, `\n ${call}`)
  100. if (next !== body) patched++
  101. out.push(...next.split('\n'))
  102. i = j
  103. }
  104. fs.writeFileSync(path, out.join('\n'))
  105. console.log('patched functions:', patched)