check-mp-pl-plan-detail-actions.mjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Mirrors web-admin/src/utils/mp-pl-plan-detail-actions.ts (no vitest in web-admin).
  3. */
  4. function splitMpPlanDetailActions(input) {
  5. const primary = []
  6. const more = []
  7. if (input.canEdit) primary.push({ kind: 'edit', label: '编辑' })
  8. if (input.canArrange) {
  9. primary.push({ kind: 'arrange', label: input.arrangeLabel })
  10. }
  11. more.push({ kind: 'copy', label: '复制' })
  12. if (input.canWait) more.push({ kind: 'wait', label: '等客户通知' })
  13. if (input.canCancelWait) more.push({ kind: 'cancel-wait', label: '取消等通知' })
  14. more.push({ kind: 'print', label: '打印单据' })
  15. if (input.canVoid) more.push({ kind: 'void', label: '作废', danger: true })
  16. while (primary.length > 2) more.unshift(primary.pop())
  17. while (primary.length < 2 && more.length) {
  18. primary.push(more.shift())
  19. }
  20. return { primary, more }
  21. }
  22. function assert(cond, msg) {
  23. if (!cond) {
  24. console.error('FAIL:', msg)
  25. process.exit(1)
  26. }
  27. }
  28. const both = splitMpPlanDetailActions({
  29. canEdit: true,
  30. canArrange: true,
  31. arrangeLabel: '安排运输',
  32. canWait: true,
  33. canCancelWait: false,
  34. canVoid: true
  35. })
  36. assert(
  37. both.primary.map((a) => a.kind).join(',') === 'edit,arrange',
  38. 'primary should be edit,arrange'
  39. )
  40. assert(
  41. both.more.map((a) => a.kind).join(',') === 'copy,wait,print,void',
  42. 'more should be copy,wait,print,void'
  43. )
  44. const onlyVoid = splitMpPlanDetailActions({
  45. canEdit: false,
  46. canArrange: false,
  47. arrangeLabel: '安排运输',
  48. canWait: false,
  49. canCancelWait: false,
  50. canVoid: true
  51. })
  52. assert(onlyVoid.primary.length === 2, 'promote more into primary until 2')
  53. assert(onlyVoid.primary[0].kind === 'copy', 'first promoted is copy')
  54. assert(onlyVoid.primary[1].kind === 'print', 'second promoted is print')
  55. assert(onlyVoid.more.map((a) => a.kind).join(',') === 'void', 'void remains in more')
  56. console.log('check-mp-pl-plan-detail-actions: ok')