|
|
|
@@ -22,7 +22,9 @@ import {
|
|
|
|
|
import { TaxProvider, TaxRate, TaxRegion, TaxRateRule } from "@models"
|
|
|
|
|
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
|
|
|
|
import { TaxRegionDTO } from "@medusajs/types"
|
|
|
|
|
import { EntityManager } from "@mikro-orm/postgresql"
|
|
|
|
|
import { uniqueRateReferenceIndexName } from "../models/tax-rate-rule"
|
|
|
|
|
import { singleDefaultRegionIndexName } from "../models/tax-rate"
|
|
|
|
|
import { countryCodeProvinceIndexName } from "../models/tax-region"
|
|
|
|
|
|
|
|
|
|
type InjectedDependencies = {
|
|
|
|
|
baseRepository: DAL.RepositoryService
|
|
|
|
@@ -105,7 +107,11 @@ export default class TaxModuleService<
|
|
|
|
|
@MedusaContext() sharedContext: Context = {}
|
|
|
|
|
): Promise<TaxTypes.TaxRateDTO[] | TaxTypes.TaxRateDTO> {
|
|
|
|
|
const input = Array.isArray(data) ? data : [data]
|
|
|
|
|
const rates = await this.create_(input, sharedContext)
|
|
|
|
|
const rates = await this.create_(input, sharedContext).catch((err) => {
|
|
|
|
|
this.handleCreateError(err)
|
|
|
|
|
this.handleCreateRulesError(err)
|
|
|
|
|
throw err
|
|
|
|
|
})
|
|
|
|
|
return Array.isArray(data) ? rates : rates[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -177,7 +183,13 @@ export default class TaxModuleService<
|
|
|
|
|
data: TaxTypes.UpdateTaxRateDTO,
|
|
|
|
|
@MedusaContext() sharedContext: Context = {}
|
|
|
|
|
): Promise<TaxTypes.TaxRateDTO | TaxTypes.TaxRateDTO[]> {
|
|
|
|
|
const rates = await this.update_(selector, data, sharedContext)
|
|
|
|
|
const rates = await this.update_(selector, data, sharedContext).catch(
|
|
|
|
|
(err) => {
|
|
|
|
|
this.handleCreateError(err)
|
|
|
|
|
this.handleCreateRulesError(err)
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
const serialized = await this.baseRepository_.serialize<
|
|
|
|
|
TaxTypes.TaxRateDTO[]
|
|
|
|
|
>(rates, { populate: true })
|
|
|
|
@@ -309,6 +321,20 @@ export default class TaxModuleService<
|
|
|
|
|
async createTaxRegions(
|
|
|
|
|
data: TaxTypes.CreateTaxRegionDTO | TaxTypes.CreateTaxRegionDTO[],
|
|
|
|
|
@MedusaContext() sharedContext: Context = {}
|
|
|
|
|
) {
|
|
|
|
|
const input = Array.isArray(data) ? data : [data]
|
|
|
|
|
const result = await this.createTaxRegions_(input, sharedContext).catch(
|
|
|
|
|
(err) => {
|
|
|
|
|
this.handleCreateRegionsError(err)
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return Array.isArray(data) ? result : result[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createTaxRegions_(
|
|
|
|
|
data: TaxTypes.CreateTaxRegionDTO[],
|
|
|
|
|
sharedContext: Context = {}
|
|
|
|
|
) {
|
|
|
|
|
const { defaultRates, regionData } =
|
|
|
|
|
this.prepareTaxRegionInputForCreate(data)
|
|
|
|
@@ -336,13 +362,10 @@ export default class TaxModuleService<
|
|
|
|
|
await this.create(rates, sharedContext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await this.baseRepository_.serialize<
|
|
|
|
|
TaxTypes.TaxRegionDTO[]
|
|
|
|
|
>(regions, {
|
|
|
|
|
populate: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return Array.isArray(data) ? result : result[0]
|
|
|
|
|
return await this.baseRepository_.serialize<TaxTypes.TaxRegionDTO[]>(
|
|
|
|
|
regions,
|
|
|
|
|
{ populate: true }
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createTaxRateRules(
|
|
|
|
@@ -360,12 +383,12 @@ export default class TaxModuleService<
|
|
|
|
|
@MedusaContext() sharedContext: Context = {}
|
|
|
|
|
) {
|
|
|
|
|
const input = Array.isArray(data) ? data : [data]
|
|
|
|
|
const rules = await this.taxRateRuleService_.create(input, sharedContext)
|
|
|
|
|
const result = await this.baseRepository_.serialize<
|
|
|
|
|
TaxTypes.TaxRateRuleDTO[]
|
|
|
|
|
>(rules, {
|
|
|
|
|
populate: true,
|
|
|
|
|
})
|
|
|
|
|
const result = await this.createTaxRateRules_(input, sharedContext).catch(
|
|
|
|
|
(err) => {
|
|
|
|
|
this.handleCreateRulesError(err)
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return Array.isArray(data) ? result : result[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -374,7 +397,13 @@ export default class TaxModuleService<
|
|
|
|
|
data: TaxTypes.CreateTaxRateRuleDTO[],
|
|
|
|
|
@MedusaContext() sharedContext: Context = {}
|
|
|
|
|
) {
|
|
|
|
|
return await this.taxRateRuleService_.create(data, sharedContext)
|
|
|
|
|
const rules = await this.taxRateRuleService_.create(data, sharedContext)
|
|
|
|
|
return await this.baseRepository_.serialize<TaxTypes.TaxRateRuleDTO[]>(
|
|
|
|
|
rules,
|
|
|
|
|
{
|
|
|
|
|
populate: true,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@InjectManager("baseRepository_")
|
|
|
|
@@ -720,6 +749,49 @@ export default class TaxModuleService<
|
|
|
|
|
return code.toLowerCase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleCreateRegionsError(err: any) {
|
|
|
|
|
if (err.constraint === countryCodeProvinceIndexName) {
|
|
|
|
|
const [countryCode, provinceCode] = err.detail
|
|
|
|
|
.split("=")[1]
|
|
|
|
|
.match(/\(([^)]+)\)/)[1]
|
|
|
|
|
.split(",")
|
|
|
|
|
|
|
|
|
|
throw new MedusaError(
|
|
|
|
|
MedusaError.Types.INVALID_DATA,
|
|
|
|
|
`You are trying to create a Tax Region for (country_code: ${countryCode.trim()}, province_code: ${provinceCode.trim()}) but one already exists.`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleCreateError(err: any) {
|
|
|
|
|
if (err.constraint === singleDefaultRegionIndexName) {
|
|
|
|
|
// err.detail = Key (tax_region_id)=(txreg_01HQX5E8GEH36ZHJWFYDAFY67P) already exists.
|
|
|
|
|
const regionId = err.detail.split("=")[1].match(/\(([^)]+)\)/)[1]
|
|
|
|
|
|
|
|
|
|
throw new MedusaError(
|
|
|
|
|
MedusaError.Types.INVALID_DATA,
|
|
|
|
|
`You are trying to create a default tax rate for region: ${regionId} which already has a default tax rate. Unset the current default rate and try again.`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleCreateRulesError(err: any) {
|
|
|
|
|
if (err.constraint === uniqueRateReferenceIndexName) {
|
|
|
|
|
// err.detail == "Key (tax_rate_id, reference_id)=(txr_01HQWRXTC0JK0F02D977WRR45T, product_id_1) already exists."
|
|
|
|
|
// We want to extract the ids from the detail string
|
|
|
|
|
// i.e. txr_01HQWRXTC0JK0F02D977WRR45T and product_id_1
|
|
|
|
|
const [taxRateId, referenceId] = err.detail
|
|
|
|
|
.split("=")[1]
|
|
|
|
|
.match(/\(([^)]+)\)/)[1]
|
|
|
|
|
.split(",")
|
|
|
|
|
|
|
|
|
|
throw new MedusaError(
|
|
|
|
|
MedusaError.Types.INVALID_DATA,
|
|
|
|
|
`You are trying to create a Tax Rate Rule for a reference that already exists. Tax Rate id: ${taxRateId.trim()}, reference id: ${referenceId.trim()}.`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @InjectTransactionManager("baseRepository_")
|
|
|
|
|
// async createProvidersOnLoad(@MedusaContext() sharedContext: Context = {}) {
|
|
|
|
|
// const providersToLoad = this.container_["tax_providers"] as ITaxProvider[]
|
|
|
|
|