merchantStaffSelection.test.mjs 6.67 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import * as merchantStaffHelpers from '../services/merchantStaffSelection.helpers.js'
import {
  STAFF_MODE,
  applySystemStaffFallbackIfNeeded,
  buildOrderPatch,
  buildSalesSingleMerchantDefault,
  getPickerLabels,
  hasStaffConflict,
  shouldEchoSystemStaff,
  shouldUseSystemStaffFallback,
  updateMerchantSelection,
  updateStaffSelection
} from '../services/merchantStaffSelection.helpers.js'

describe('merchantStaffSelection', () => {
  it('buildOrderPatch maps sales fields', () => {
    const patch = buildOrderPatch(
      STAFF_MODE.SALES,
      { merchantId: 'm1', merchantName: '门店A' },
      { uuid: 'u1', staffName: '张三' }
    )
    assert.deepEqual(patch, {
      merchantId: 'm1',
      merchantName: '门店A',
      userId: 'u1',
      employeeName: '张三'
    })
  })

  it('buildOrderPatch normalizes raw sales staff fields at the write boundary', () => {
    const patch = buildOrderPatch(
      STAFF_MODE.SALES,
      { merchantId: 'm1', merchantName: '门店A' },
      {
        uuid: '',
        userId: 'u-raw-1',
        name: '原始接口销售顾问'
      }
    )

    assert.deepEqual(patch, {
      merchantId: 'm1',
      merchantName: '门店A',
      userId: 'u-raw-1',
      employeeName: '原始接口销售顾问'
    })
  })

  it('buildOrderPatch maps conversion fields', () => {
    const patch = buildOrderPatch(
      STAFF_MODE.CONVERSION,
      { merchantId: 'yq1', merchantName: '园区店' },
      { uuid: 's1', staffName: '李四' }
    )
    assert.deepEqual(patch, {
      sellerMerchantId: 'yq1',
      sellerMerchantName: '园区店',
      sellerUserId: 's1',
      sellerName: '李四'
    })
  })

  it('getPickerLabels adjusts sales title for points multi role', () => {
    const labels = getPickerLabels(STAFF_MODE.SALES, 'Y')
    assert.equal(labels.popupTitle, '销售顾问/录单员')
    assert.equal(labels.staffTitle, '选择销售顾问/录单员')
  })

  it('hasStaffConflict blocks same person for sales and delivery', () => {
    const orderObj = { deliveryUserId: 'u2', userId: 'u1' }
    assert.equal(hasStaffConflict(STAFF_MODE.SALES, orderObj, 'u2'), true)
    assert.equal(hasStaffConflict(STAFF_MODE.DELIVERY, orderObj, 'u1'), true)
    assert.equal(hasStaffConflict(STAFF_MODE.CONVERSION, orderObj, 'u1'), false)
  })

  it('updateMerchantSelection marks only selected merchant active', () => {
    const list = updateMerchantSelection(
      [
        { merchantId: 'm1', merchantName: 'A', active: true },
        { merchantId: 'm2', merchantName: 'B', active: false }
      ],
      'm2'
    )
    assert.equal(list[0].active, false)
    assert.equal(list[1].active, true)
  })

  it('buildSalesSingleMerchantDefault uses system staff when list empty', () => {
    const patch = buildSalesSingleMerchantDefault(
      [{ merchantId: 'm1', merchantName: '唯一门店' }],
      []
    )
    assert.equal(patch.merchantId, 'm1')
    assert.equal(patch.userId, -1)
    assert.equal(patch.employeeName, '系统')
  })

  it('updateStaffSelection marks only selected staff active', () => {
    const list = updateStaffSelection(
      [
        { uuid: 'u1', staffName: 'A', active: true },
        { uuid: 'u2', staffName: 'B', active: false }
      ],
      'u2'
    )
    assert.equal(list[0].active, false)
    assert.equal(list[1].active, true)
  })

  it('shouldUseSystemStaffFallback requires single merchant and empty order merchant', () => {
    const context = {
      orderObj: {},
      orderCreateType: 'createdOrder',
      merchantList: [{ merchantId: 'm1' }]
    }
    assert.equal(shouldUseSystemStaffFallback(STAFF_MODE.SALES, context), true)
    assert.equal(
      shouldUseSystemStaffFallback(STAFF_MODE.SALES, {
        ...context,
        merchantList: [{ merchantId: 'm1' }, { merchantId: 'm2' }]
      }),
      false
    )
    assert.equal(
      shouldUseSystemStaffFallback(STAFF_MODE.SALES, {
        ...context,
        orderObj: { merchantId: 'm1' }
      }),
      false
    )
    assert.equal(
      shouldUseSystemStaffFallback(STAFF_MODE.SALES, {
        ...context,
        orderCreateType: 'additionRecordOrder'
      }),
      false
    )
  })

  it('applySystemStaffFallbackIfNeeded injects system for empty staff list', () => {
    const result = applySystemStaffFallbackIfNeeded(STAFF_MODE.SALES, [], {
      orderObj: {},
      orderCreateType: 'createdOrder',
      merchantList: [{ merchantId: 'm1' }]
    })
    assert.equal(result.length, 1)
    assert.equal(result[0].uuid, -1)
    assert.equal(result[0].staffName, '系统')
  })

  it('applySystemStaffFallbackIfNeeded does not inject system during search', () => {
    const result = applySystemStaffFallbackIfNeeded(STAFF_MODE.SALES, [], {
      orderObj: {},
      orderCreateType: 'createdOrder',
      merchantList: [{ merchantId: 'm1' }],
      keyWord: '张三'
    })
    assert.deepEqual(result, [])
  })

  it('shouldEchoSystemStaff detects userId -1', () => {
    assert.equal(
      shouldEchoSystemStaff(STAFF_MODE.SALES, { userId: -1 }),
      true
    )
    assert.equal(
      shouldEchoSystemStaff(STAFF_MODE.SALES, { userId: 'u1' }),
      false
    )
  })

  it('applySystemStaffFallbackIfNeeded echoes system staff for userId -1', () => {
    const result = applySystemStaffFallbackIfNeeded(STAFF_MODE.SALES, [], {
      orderObj: { userId: -1, merchantId: 'm1' },
      orderCreateType: 'createdOrder',
      merchantList: [{ merchantId: 'm1' }, { merchantId: 'm2' }]
    })
    assert.equal(result[0].staffName, '系统')
  })

  it('低价单销售顾问使用前一步选中的园区店和 4S 店', () => {
    assert.equal(
      typeof merchantStaffHelpers.resolveStaffMerchantScope,
      'function',
      '尚未实现 resolveStaffMerchantScope'
    )

    const scope = merchantStaffHelpers.resolveStaffMerchantScope(
      {
        lowPriceFlag: 1,
        merchantId: 44210999,
        yqMerchantId: 44211999,
        sellerMerchantId: 44211999
      },
      {
        parkMerchantId: 44211025,
        shopMerchantId: 44210895
      }
    )

    assert.deepEqual(scope, {
      parkMerchantId: 44211999,
      shopMerchantId: 44210999
    })
  })

  it('非低价单销售顾问继续使用原用户门店上下文', () => {
    assert.equal(
      typeof merchantStaffHelpers.resolveStaffMerchantScope,
      'function',
      '尚未实现 resolveStaffMerchantScope'
    )

    const scope = merchantStaffHelpers.resolveStaffMerchantScope(
      {
        merchantId: 44210999,
        sellerMerchantId: 44211999
      },
      {
        parkMerchantId: 44211025,
        shopMerchantId: 44210895
      }
    )

    assert.deepEqual(scope, {
      parkMerchantId: 44211025,
      shopMerchantId: 44210895
    })
  })
})