import { getRepository } from 'typeorm'
import { ReleaseGood } from '@things-factory/sales-base'

import { Domain, ListParam } from '@things-factory/shell'

export const shippingProviders = {
  async shippingProviders(_: any, params: ListParam, context: any) {
    try {
      const { domain }: { domain: Domain } = context.state

      const items: any[] = await getRepository(ReleaseGood).query(`
       SELECT DISTINCT case when transporter = 'EMS' then 'EFMX' else transporter end from
        (SELECT DISTINCT UPPER(transporter) as transporter FROM release_goods WHERE domain_id = '${domain.id}' AND transporter IS NOT null AND transporter != '' AND type = 'b2c'
        UNION ALL
        SELECT DISTINCT UPPER(transporter) as transporter FROM order_packages WHERE domain_id = '${domain.id}' AND transporter IS NOT null AND transporter != '')foo`)

      return {
        items: items
      }
    } catch (error) {
      throw error
    }
  }
}
