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 { LineItem } from "../models/line-item"
|
||||||
import { Region } from "../models/region"
|
import { Region } from "../models/region"
|
||||||
import { Address } from "../models/address"
|
import { Address } from "../models/address"
|
||||||
@@ -57,3 +59,23 @@ export interface ITaxService {
|
|||||||
context: TaxCalculationContext
|
context: TaxCalculationContext
|
||||||
): Promise<ProviderTaxLine[]>
|
): 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")
|
const entityManager = container.resolve("manager")
|
||||||
|
|
||||||
await entityManager.transaction(async manager => {
|
await entityManager.transaction(async (manager) => {
|
||||||
const countryRepo = manager.getCustomRepository(countryRepository)
|
const countryRepo = manager.getCustomRepository(countryRepository)
|
||||||
const hasCountries = await countryRepo.findOne()
|
const hasCountries = await countryRepo.findOne()
|
||||||
if (!hasCountries) {
|
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 currencyRepo = manager.getCustomRepository(currencyRepository)
|
||||||
const hasCurrencies = await currencyRepo.findOne()
|
const hasCurrencies = await currencyRepo.findOne()
|
||||||
if (!hasCurrencies) {
|
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()
|
await storeService.withTransaction(manager).create()
|
||||||
|
|
||||||
let payIds
|
let payIds
|
||||||
@@ -89,7 +89,7 @@ export default async ({ container }) => {
|
|||||||
const payProviders =
|
const payProviders =
|
||||||
silentResolution(container, "paymentProviders", logger) || []
|
silentResolution(container, "paymentProviders", logger) || []
|
||||||
|
|
||||||
payIds = payProviders.map(p => p.getIdentifier())
|
payIds = payProviders.map((p) => p.getIdentifier())
|
||||||
await pProviderService.registerInstalledProviders(payIds)
|
await pProviderService.registerInstalledProviders(payIds)
|
||||||
|
|
||||||
let notiIds
|
let notiIds
|
||||||
@@ -98,7 +98,7 @@ export default async ({ container }) => {
|
|||||||
const notiProviders =
|
const notiProviders =
|
||||||
silentResolution(container, "notificationProviders", logger) || []
|
silentResolution(container, "notificationProviders", logger) || []
|
||||||
|
|
||||||
notiIds = notiProviders.map(p => p.getIdentifier())
|
notiIds = notiProviders.map((p) => p.getIdentifier())
|
||||||
await nProviderService.registerInstalledProviders(notiIds)
|
await nProviderService.registerInstalledProviders(notiIds)
|
||||||
|
|
||||||
let fulfilIds
|
let fulfilIds
|
||||||
@@ -107,9 +107,18 @@ export default async ({ container }) => {
|
|||||||
const fulfilProviders =
|
const fulfilProviders =
|
||||||
silentResolution(container, "fulfillmentProviders", logger) || []
|
silentResolution(container, "fulfillmentProviders", logger) || []
|
||||||
|
|
||||||
fulfilIds = fulfilProviders.map(p => p.getIdentifier())
|
fulfilIds = fulfilProviders.map((p) => p.getIdentifier())
|
||||||
await fProviderService.registerInstalledProviders(fulfilIds)
|
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).createDefault()
|
||||||
await profileService.withTransaction(manager).createGiftCardDefault()
|
await profileService.withTransaction(manager).createGiftCardDefault()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import fs from "fs"
|
|||||||
import { asValue, asClass, asFunction, aliasTo } from "awilix"
|
import { asValue, asClass, asFunction, aliasTo } from "awilix"
|
||||||
import { sync as existsSync } from "fs-exists-cached"
|
import { sync as existsSync } from "fs-exists-cached"
|
||||||
|
|
||||||
|
import { AbstractTaxService } from "../interfaces/tax-service"
|
||||||
import { isTaxCalculationStrategy } from "../interfaces/tax-calculation-strategy"
|
import { isTaxCalculationStrategy } from "../interfaces/tax-calculation-strategy"
|
||||||
import formatRegistrationName from "../utils/format-registration-name"
|
import formatRegistrationName from "../utils/format-registration-name"
|
||||||
|
|
||||||
@@ -317,6 +318,18 @@ async function registerServices(pluginDetails, container) {
|
|||||||
),
|
),
|
||||||
[`searchService`]: aliasTo(name),
|
[`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 {
|
} else {
|
||||||
container.register({
|
container.register({
|
||||||
[name]: asFunction(
|
[name]: asFunction(
|
||||||
@@ -430,7 +443,7 @@ function resolvePlugin(pluginName) {
|
|||||||
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
|
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
|
||||||
)
|
)
|
||||||
const name = packageJSON.name || pluginName
|
const name = packageJSON.name || pluginName
|
||||||
//warnOnIncompatiblePeerDependency(name, packageJSON)
|
// warnOnIncompatiblePeerDependency(name, packageJSON)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
resolve: resolvedPath,
|
resolve: resolvedPath,
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
import { BaseService } from "medusa-interfaces"
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ITaxService,
|
AbstractTaxService,
|
||||||
ItemTaxCalculationLine,
|
ItemTaxCalculationLine,
|
||||||
ShippingTaxCalculationLine,
|
ShippingTaxCalculationLine,
|
||||||
TaxCalculationContext,
|
TaxCalculationContext,
|
||||||
} from "../interfaces/tax-service"
|
} from "../interfaces/tax-service"
|
||||||
import { ProviderTaxLine } from "../types/tax-service"
|
import { ProviderTaxLine } from "../types/tax-service"
|
||||||
|
|
||||||
class SystemTaxService extends BaseService implements ITaxService {
|
class SystemTaxService extends AbstractTaxService {
|
||||||
static identifier = "system"
|
static identifier = "system"
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|||||||
@@ -428,6 +428,15 @@ class TaxProviderService extends BaseService {
|
|||||||
|
|
||||||
return null
|
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
|
export default TaxProviderService
|
||||||
|
|||||||
Reference in New Issue
Block a user