Skip to content

Commit

Permalink
Merge branch 'develop' into feat/order-module-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues authored Feb 20, 2024
2 parents 20dcb78 + 137cc0e commit 8b882f1
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/admin-next/dashboard/src/lib/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export const currencies: Record<string, CurrencyInfo> = {
INR: {
code: "INR",
name: "Indian Rupee",
symbol_native: "টকা",
symbol_native: "",
decimal_digits: 2,
},
IQD: {
Expand Down
2 changes: 1 addition & 1 deletion packages/admin-ui/ui/src/utils/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export const currencies: CurrenciesType = {
INR: {
symbol: "Rs",
name: "Indian Rupee",
symbol_native: "টকা",
symbol_native: "",
decimal_digits: 2,
rounding: 0,
code: "INR",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
UpdateServiceZoneDTO,
} from "@medusajs/types"
import { GeoZoneType } from "@medusajs/utils"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"

jest.setTimeout(100000)

Expand Down Expand Up @@ -1004,19 +1004,24 @@ moduleIntegrationTestRunner({
)

const updatedFulfillmentSets = await service.update(updateData)
const fullfillmentSets = await service.list({
id: updateData.map((ud) => ud.id),
})

expect(updatedFulfillmentSets).toHaveLength(2)

let i = 0
for (const data_ of updateData) {
expect(updatedFulfillmentSets[i]).toEqual(
const currentFullfillmentSet = fullfillmentSets.find(
(fs) => fs.id === data_.id
)

expect(currentFullfillmentSet).toEqual(
expect.objectContaining({
id: createdFulfillmentSets[i].id,
id: data_.id,
name: data_.name,
type: data_.type,
})
)
++i
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/utils/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export const currencies: Record<string, Currency> = {
INR: {
symbol: "Rs",
name: "Indian Rupee",
symbol_native: "টকা",
symbol_native: "",
decimal_digits: 2,
rounding: 0,
code: "INR",
Expand Down
46 changes: 46 additions & 0 deletions packages/tax/integration-tests/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,50 @@ describe("TaxModuleService", function () {
])
)
})

it("should create a tax rate rule", async () => {
const [region] = await service.createTaxRegions([
{
country_code: "US",
default_tax_rate: {
name: "Test Rate",
rate: 0.2,
},
},
])

const rate = await service.create({
tax_region_id: region.id,
name: "Shipping Rate",
rate: 8.23,
})

await service.createTaxRateRules([
{
tax_rate_id: rate.id,
reference: "product",
reference_id: "prod_1234",
},
])

const listedRules = await service.listTaxRateRules(
{},
{
relations: ["tax_rate"],
}
)
expect(listedRules).toEqual(
expect.arrayContaining([
expect.objectContaining({
reference: "product",
reference_id: "prod_1234",
tax_rate: expect.objectContaining({
tax_region_id: region.id,
name: "Shipping Rate",
rate: 8.23,
}),
}),
])
)
})
})
1 change: 1 addition & 0 deletions packages/tax/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as TaxRate } from "./tax-rate"
export { default as TaxRegion } from "./tax-region"
export { default as TaxRateRule } from "./tax-rate-rule"
55 changes: 55 additions & 0 deletions packages/tax/src/models/tax-rate-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
Cascade,
Entity,
ManyToOne,
PrimaryKey,
PrimaryKeyProp,
Property,
} from "@mikro-orm/core"
import TaxRate from "./tax-rate"

const TABLE_NAME = "tax_rate_rule"

const taxRateIdIndexName = "IDX_tax_rate_rule_tax_rate_id"

@Entity({ tableName: TABLE_NAME })
export default class TaxRateRule {
@PrimaryKey({ columnType: "text" })
tax_rate_id!: string

@PrimaryKey({ columnType: "text" })
reference_id!: string;

[PrimaryKeyProp]?: ["tax_rate_id", "reference_id"]

@Property({ columnType: "text" })
reference: string

@ManyToOne(() => TaxRate, {
fieldName: "tax_rate_id",
index: taxRateIdIndexName,
cascade: [Cascade.REMOVE, Cascade.PERSIST],
})
tax_rate: TaxRate

@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null

@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date

@Property({ columnType: "text", nullable: true })
created_by: string | null = null
}
48 changes: 43 additions & 5 deletions packages/tax/src/services/tax-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,46 @@ import {
MedusaContext,
ModulesSdkUtils,
} from "@medusajs/utils"
import { TaxRate, TaxRegion } from "@models"
import { TaxRate, TaxRegion, TaxRateRule } from "@models"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { TaxRegionDTO } from "@medusajs/types"

type InjectedDependencies = {
baseRepository: DAL.RepositoryService
taxRateService: ModulesSdkTypes.InternalModuleService<any>
taxRegionService: ModulesSdkTypes.InternalModuleService<any>
taxRateRuleService: ModulesSdkTypes.InternalModuleService<any>
}

const generateForModels = [TaxRegion, TaxRateRule]

export default class TaxModuleService<
TTaxRate extends TaxRate = TaxRate,
TTaxRegion extends TaxRegion = TaxRegion
TTaxRegion extends TaxRegion = TaxRegion,
TTaxRateRule extends TaxRateRule = TaxRateRule
>
extends ModulesSdkUtils.abstractModuleServiceFactory<
InjectedDependencies,
TaxTypes.TaxRateDTO,
{ TaxRegion: { dto: TaxTypes.TaxRegionDTO } }
>(TaxRate, [TaxRegion], entityNameToLinkableKeysMap)
{
TaxRegion: { dto: TaxTypes.TaxRegionDTO }
TaxRateRule: { dto: TaxTypes.TaxRateRuleDTO }
}
>(TaxRate, generateForModels, entityNameToLinkableKeysMap)
implements ITaxModuleService
{
protected baseRepository_: DAL.RepositoryService
protected taxRateService_: ModulesSdkTypes.InternalModuleService<TTaxRate>
protected taxRegionService_: ModulesSdkTypes.InternalModuleService<TTaxRegion>
protected taxRateRuleService_: ModulesSdkTypes.InternalModuleService<TTaxRateRule>

constructor(
{ baseRepository, taxRateService, taxRegionService }: InjectedDependencies,
{
baseRepository,
taxRateService,
taxRegionService,
taxRateRuleService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
// @ts-ignore
Expand All @@ -47,6 +61,7 @@ export default class TaxModuleService<
this.baseRepository_ = baseRepository
this.taxRateService_ = taxRateService
this.taxRegionService_ = taxRegionService
this.taxRateRuleService_ = taxRateRuleService
}

__joinerConfig(): ModuleJoinerConfig {
Expand Down Expand Up @@ -85,6 +100,7 @@ export default class TaxModuleService<
return await this.taxRateService_.create(data, sharedContext)
}

@InjectManager("baseRepository_")
async createTaxRegions(
data: TaxTypes.CreateTaxRegionDTO[],
@MedusaContext() sharedContext: Context = {}
Expand Down Expand Up @@ -128,4 +144,26 @@ export default class TaxModuleService<
}
)
}

@InjectManager("baseRepository_")
async createTaxRateRules(
data: TaxTypes.CreateTaxRateRuleDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TaxTypes.TaxRateRuleDTO[]> {
const rules = await this.taxRateRuleService_.create(data, sharedContext)
const result = await this.baseRepository_.serialize<
TaxTypes.TaxRateRuleDTO[]
>(rules, {
populate: true,
})
return result
}

@InjectTransactionManager("baseRepository_")
async createTaxRateRules_(
data: TaxTypes.CreateTaxRateRuleDTO[],
@MedusaContext() sharedContext: Context = {}
) {
return await this.taxRateRuleService_.create(data, sharedContext)
}
}
26 changes: 26 additions & 0 deletions packages/types/src/tax/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,29 @@ export interface FilterableTaxRegionProps
updated_at?: OperatorMap<string>
created_by?: string | string[] | OperatorMap<string>
}

export interface TaxRateRuleDTO {
reference: string
reference_id: string
tax_rate_id: string
tax_rate?: TaxRateDTO
metadata?: Record<string, unknown> | null
created_at: string | Date
updated_at: string | Date
created_by: string | null
}

export interface FilterableTaxRateRuleProps
extends BaseFilterable<FilterableTaxRateRuleProps> {
reference?: string | string[] | OperatorMap<string>
reference_id?: string | string[] | OperatorMap<string>
tax_rate_id?: string | string[] | OperatorMap<string>
tax_rate?: FilterableTaxRateProps
metadata?:
| Record<string, unknown>
| Record<string, unknown>[]
| OperatorMap<Record<string, unknown>>
created_at?: OperatorMap<string>
updated_at?: OperatorMap<string>
created_by?: string | string[] | OperatorMap<string>
}
8 changes: 8 additions & 0 deletions packages/types/src/tax/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ export interface CreateTaxRegionDTO {
metadata?: Record<string, unknown>
}
}

export interface CreateTaxRateRuleDTO {
reference: string
reference_id: string
tax_rate_id: string
metadata?: Record<string, unknown>
created_by?: string
}
19 changes: 18 additions & 1 deletion packages/types/src/tax/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import {
FilterableTaxRateProps,
TaxRateDTO,
TaxRegionDTO,
TaxRateRuleDTO,
FilterableTaxRateRuleProps,
} from "./common"
import { CreateTaxRateDTO, CreateTaxRegionDTO } from "./mutations"
import {
CreateTaxRateRuleDTO,
CreateTaxRateDTO,
CreateTaxRegionDTO,
} from "./mutations"

export interface ITaxModuleService extends IModuleService {
retrieve(
Expand Down Expand Up @@ -47,4 +53,15 @@ export interface ITaxModuleService extends IModuleService {
config?: FindConfig<TaxRegionDTO>,
sharedContext?: Context
): Promise<TaxRegionDTO[]>

createTaxRateRules(
data: CreateTaxRateRuleDTO[],
sharedContext?: Context
): Promise<TaxRateRuleDTO[]>

listTaxRateRules(
filters?: FilterableTaxRateRuleProps,
config?: FindConfig<TaxRateRuleDTO>,
sharedContext?: Context
): Promise<TaxRateRuleDTO[]>
}
2 changes: 1 addition & 1 deletion packages/utils/src/defaults/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export const defaultCurrencies: Record<string, Currency> = {
INR: {
symbol: "Rs",
name: "Indian Rupee",
symbol_native: "টকা",
symbol_native: "",
decimal_digits: 2,
rounding: 0,
code: "INR",
Expand Down

0 comments on commit 8b882f1

Please sign in to comment.