|
1
|
import { DAL, ITaxProvider, Logger, TaxTypes } from "@medusajs/framework/types" |
|
2
|
import { ModulesSdkUtils } from "@medusajs/framework/utils" |
|
3
|
|
|
4
|
import TaxProvider from "../models/tax-provider" |
|
5
|
|
|
6
|
type InjectedDependencies = { |
|
7
|
logger?: Logger |
|
8
|
taxProviderRepository: DAL.RepositoryService |
|
9
|
[key: `tp_${string}`]: ITaxProvider |
|
10
|
} |
|
11
|
|
|
12
|
export default class TaxProviderService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>( |
|
13
|
TaxProvider |
|
14
|
) { |
|
15
|
#logger: Logger |
|
16
|
|
|
17
|
constructor(container: InjectedDependencies) { |
|
18
|
super(container) |
|
19
|
this.#logger = container["logger"] |
|
20
|
? container.logger |
|
21
|
: (console as unknown as Logger) |
|
22
|
} |
|
23
|
|
|
24
|
retrieveProvider(providerId: string): ITaxProvider { |
|
25
|
try { |
|
26
|
return this.__container__[providerId] as ITaxProvider |
|
27
|
} catch (err) { |
|
28
|
if (err.name === "AwilixResolutionError") { |
|
29
|
const errMessage = ` |
|
30
|
Unable to retrieve the tax provider with id: ${providerId} |
|
31
|
Please make sure that the provider is registered in the container and it is configured correctly in your project configuration file.` |
|
32
|
|
|
33
|
// Log full error for debugging |
|
34
|
this.#logger.error(`AwilixResolutionError: ${err.message}`, err) |
|
35
|
|
|
36
|
throw new Error(errMessage) |
|
37
|
} |
|
38
|
|
|
39
|
const errMessage = `Unable to retrieve the tax provider with id: ${providerId}, the following error occurred: ${err.message}` |
|
40
|
this.#logger.error(errMessage) |
|
41
|
|
|
42
|
throw new Error(errMessage) |
|
43
|
} |
|
44
|
} |
|
45
|
|
|
46
|
async getTaxLines( |
|
47
|
providerId: string, |
|
48
|
itemLines: TaxTypes.ItemTaxCalculationLine[], |
|
49
|
shippingLines: TaxTypes.ShippingTaxCalculationLine[], |
|
50
|
context: TaxTypes.TaxCalculationContext |
|
51
|
): Promise<(TaxTypes.ItemTaxLineDTO | TaxTypes.ShippingTaxLineDTO)[]> { |
|
52
|
const provider = this.retrieveProvider(providerId) |
|
53
|
return provider.getTaxLines(itemLines, shippingLines, context) |
|
54
|
} |
|
55
|
} |