fix: add tax service registration (#1225)
* fix: add tax service registration * fix: cleanup
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
|
||||
import { LineItem } from "../models/line-item"
|
||||
import { Region } from "../models/region"
|
||||
import { Address } from "../models/address"
|
||||
@@ -57,3 +59,23 @@ export interface ITaxService {
|
||||
context: TaxCalculationContext
|
||||
): Promise<ProviderTaxLine[]>
|
||||
}
|
||||
|
||||
export abstract class AbstractTaxService
|
||||
extends BaseService
|
||||
implements ITaxService
|
||||
{
|
||||
protected static identifier: string
|
||||
|
||||
public getIdentifier(): string {
|
||||
if (!(<typeof AbstractTaxService>this.constructor).identifier) {
|
||||
throw new Error('Missing static property "identifier".')
|
||||
}
|
||||
return (<typeof AbstractTaxService>this.constructor).identifier
|
||||
}
|
||||
|
||||
public abstract getTaxLines(
|
||||
itemLines: ItemTaxCalculationLine[],
|
||||
shippingLines: ShippingTaxCalculationLine[],
|
||||
context: TaxCalculationContext
|
||||
): Promise<ProviderTaxLine[]>
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export default async ({ container }) => {
|
||||
|
||||
const entityManager = container.resolve("manager")
|
||||
|
||||
await entityManager.transaction(async manager => {
|
||||
await entityManager.transaction(async (manager) => {
|
||||
const countryRepo = manager.getCustomRepository(countryRepository)
|
||||
const hasCountries = await countryRepo.findOne()
|
||||
if (!hasCountries) {
|
||||
@@ -63,7 +63,7 @@ export default async ({ container }) => {
|
||||
}
|
||||
})
|
||||
|
||||
await entityManager.transaction(async manager => {
|
||||
await entityManager.transaction(async (manager) => {
|
||||
const currencyRepo = manager.getCustomRepository(currencyRepository)
|
||||
const hasCurrencies = await currencyRepo.findOne()
|
||||
if (!hasCurrencies) {
|
||||
@@ -80,7 +80,7 @@ export default async ({ container }) => {
|
||||
}
|
||||
})
|
||||
|
||||
await entityManager.transaction(async manager => {
|
||||
await entityManager.transaction(async (manager) => {
|
||||
await storeService.withTransaction(manager).create()
|
||||
|
||||
let payIds
|
||||
@@ -89,7 +89,7 @@ export default async ({ container }) => {
|
||||
const payProviders =
|
||||
silentResolution(container, "paymentProviders", logger) || []
|
||||
|
||||
payIds = payProviders.map(p => p.getIdentifier())
|
||||
payIds = payProviders.map((p) => p.getIdentifier())
|
||||
await pProviderService.registerInstalledProviders(payIds)
|
||||
|
||||
let notiIds
|
||||
@@ -98,7 +98,7 @@ export default async ({ container }) => {
|
||||
const notiProviders =
|
||||
silentResolution(container, "notificationProviders", logger) || []
|
||||
|
||||
notiIds = notiProviders.map(p => p.getIdentifier())
|
||||
notiIds = notiProviders.map((p) => p.getIdentifier())
|
||||
await nProviderService.registerInstalledProviders(notiIds)
|
||||
|
||||
let fulfilIds
|
||||
@@ -107,9 +107,18 @@ export default async ({ container }) => {
|
||||
const fulfilProviders =
|
||||
silentResolution(container, "fulfillmentProviders", logger) || []
|
||||
|
||||
fulfilIds = fulfilProviders.map(p => p.getIdentifier())
|
||||
fulfilIds = fulfilProviders.map((p) => p.getIdentifier())
|
||||
await fProviderService.registerInstalledProviders(fulfilIds)
|
||||
|
||||
let taxIds
|
||||
const tProviderService = container.resolve("taxProviderService")
|
||||
|
||||
const taxProviders =
|
||||
silentResolution(container, "taxProviders", logger) || []
|
||||
|
||||
taxIds = taxProviders.map((p) => p.getIdentifier())
|
||||
await tProviderService.registerInstalledProviders(taxIds)
|
||||
|
||||
await profileService.withTransaction(manager).createDefault()
|
||||
await profileService.withTransaction(manager).createGiftCardDefault()
|
||||
})
|
||||
|
||||
@@ -16,6 +16,7 @@ import fs from "fs"
|
||||
import { asValue, asClass, asFunction, aliasTo } from "awilix"
|
||||
import { sync as existsSync } from "fs-exists-cached"
|
||||
|
||||
import { AbstractTaxService } from "../interfaces/tax-service"
|
||||
import { isTaxCalculationStrategy } from "../interfaces/tax-calculation-strategy"
|
||||
import formatRegistrationName from "../utils/format-registration-name"
|
||||
|
||||
@@ -317,6 +318,18 @@ async function registerServices(pluginDetails, container) {
|
||||
),
|
||||
[`searchService`]: aliasTo(name),
|
||||
})
|
||||
} else if (loaded.prototype instanceof AbstractTaxService) {
|
||||
container.registerAdd(
|
||||
"taxProviders",
|
||||
asFunction((cradle) => new loaded(cradle, pluginDetails.options))
|
||||
)
|
||||
|
||||
container.register({
|
||||
[name]: asFunction(
|
||||
(cradle) => new loaded(cradle, pluginDetails.options)
|
||||
).singleton(),
|
||||
[`tp_${loaded.identifier}`]: aliasTo(name),
|
||||
})
|
||||
} else {
|
||||
container.register({
|
||||
[name]: asFunction(
|
||||
@@ -430,7 +443,7 @@ function resolvePlugin(pluginName) {
|
||||
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
|
||||
)
|
||||
const name = packageJSON.name || pluginName
|
||||
//warnOnIncompatiblePeerDependency(name, packageJSON)
|
||||
// warnOnIncompatiblePeerDependency(name, packageJSON)
|
||||
|
||||
return {
|
||||
resolve: resolvedPath,
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
|
||||
import {
|
||||
ITaxService,
|
||||
AbstractTaxService,
|
||||
ItemTaxCalculationLine,
|
||||
ShippingTaxCalculationLine,
|
||||
TaxCalculationContext,
|
||||
} from "../interfaces/tax-service"
|
||||
import { ProviderTaxLine } from "../types/tax-service"
|
||||
|
||||
class SystemTaxService extends BaseService implements ITaxService {
|
||||
class SystemTaxService extends AbstractTaxService {
|
||||
static identifier = "system"
|
||||
|
||||
constructor() {
|
||||
|
||||
@@ -428,6 +428,15 @@ class TaxProviderService extends BaseService {
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async registerInstalledProviders(providers: string[]): Promise<void> {
|
||||
const model = this.manager_.getCustomRepository(this.taxProviderRepo_)
|
||||
model.update({}, { is_installed: false })
|
||||
for (const p of providers) {
|
||||
const n = model.create({ id: p, is_installed: true })
|
||||
await model.save(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default TaxProviderService
|
||||
|
||||
Reference in New Issue
Block a user