Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tax): tax module scaffolding #6417

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/modules-sdk/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export enum Modules {
PRODUCT = "productService",
PROMOTION = "promotion",
SALES_CHANNEL = "salesChannel",
TAX = "tax",
FULFILLMENT = "fulfillment",
STOCK_LOCATION = "stockLocationService",
USER = "user",
Expand All @@ -41,6 +42,7 @@ export enum ModuleRegistrationName {
SALES_CHANNEL = "salesChannelModuleService",
FULFILLMENT = "fulfillmentModuleService",
STOCK_LOCATION = "stockLocationService",
TAX = "taxModuleService",
USER = "userModuleService",
WORKFLOW_ENGINE = "workflowsModuleService",
REGION = "regionModuleService",
Expand All @@ -62,6 +64,7 @@ export const MODULE_PACKAGE_NAMES = {
[Modules.SALES_CHANNEL]: "@medusajs/sales-channel",
[Modules.FULFILLMENT]: "@medusajs/fulfillment",
[Modules.STOCK_LOCATION]: "@medusajs/stock-location",
[Modules.TAX]: "@medusajs/tax",
[Modules.USER]: "@medusajs/user",
[Modules.WORKFLOW_ENGINE]: "@medusajs/workflow-engine-inmemory",
[Modules.REGION]: "@medusajs/region",
Expand Down Expand Up @@ -292,6 +295,19 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.TAX]: {
key: Modules.TAX,
registrationName: ModuleRegistrationName.TAX,
defaultPackage: false,
label: upperCaseFirst(ModuleRegistrationName.TAX),
isRequired: false,
isQueryable: true,
dependencies: ["logger", "eventBusService"],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}

export const MODULE_DEFINITIONS: ModuleDefinition[] =
Expand Down
2 changes: 1 addition & 1 deletion packages/order/integration-tests/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mikroOrmEntities = Models as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
mikroOrmEntities,
null,
process.env.MEDUSA_FULFILLMENT_DB_SCHEMA
process.env.MEDUSA_ORDER_DB_SCHEMA
)

export const DB_URL = TestDatabaseUtils.getDatabaseURL()
6 changes: 6 additions & 0 deletions packages/tax/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/dist
node_modules
.DS_store
.env*
.env
*.sql
1 change: 1 addition & 0 deletions packages/tax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Tax Module
5 changes: 5 additions & 0 deletions packages/tax/integration-tests/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("noop", function () {
it("should run", function () {
expect(true).toBe(true)
})
})
6 changes: 6 additions & 0 deletions packages/tax/integration-tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (typeof process.env.DB_TEMP_NAME === "undefined") {
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
process.env.DB_TEMP_NAME = `medusa-tax-integration-${tempName}`
}

process.env.MEDUSA_TAX_DB_SCHEMA = "public"
3 changes: 3 additions & 0 deletions packages/tax/integration-tests/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { JestUtils } from "medusa-test-utils"

JestUtils.afterAllHookDropDatabase()
13 changes: 13 additions & 0 deletions packages/tax/integration-tests/utils/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TestDatabaseUtils } from "medusa-test-utils"

import * as Models from "@models"

const mikroOrmEntities = Models as unknown as any[]

export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
mikroOrmEntities,
null,
process.env.MEDUSA_TAX_DB_SCHEMA
)

export const DB_URL = TestDatabaseUtils.getDatabaseURL()
33 changes: 33 additions & 0 deletions packages/tax/integration-tests/utils/get-init-module-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"

import { DB_URL } from "./database"

export function getInitModuleConfig() {
const moduleOptions = {
defaultAdapterOptions: {
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_TAX_DB_SCHEMA,
},
},
}

const injectedDependencies = {}

const modulesConfig_ = {
[Modules.TAX]: {
definition: ModulesDefinition[Modules.TAX],
options: moduleOptions,
},
}

return {
injectedDependencies,
modulesConfig: modulesConfig_,
databaseConfig: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_TAX_DB_SCHEMA,
},
joinerConfig: [],
}
}
2 changes: 2 additions & 0 deletions packages/tax/integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./database"
export * from "./get-init-module-config"
22 changes: 22 additions & 0 deletions packages/tax/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
moduleNameMapper: {
"^@models": "<rootDir>/src/models",
"^@services": "<rootDir>/src/services",
"^@repositories": "<rootDir>/src/repositories",
"^@types": "<rootDir>/src/types",
},
transform: {
"^.+\\.[jt]s?$": [
"ts-jest",
{
tsConfig: "tsconfig.spec.json",
isolatedModules: true,
},
],
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`],
modulePathIgnorePatterns: ["dist/"],
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
}
12 changes: 12 additions & 0 deletions packages/tax/mikro-orm.config.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TSMigrationGenerator } from "@medusajs/utils"
import * as entities from "./src/models"

module.exports = {
entities: Object.values(entities),
schema: "public",
clientUrl: "postgres://postgres@localhost/medusa-tax",
type: "postgresql",
migrations: {
generator: TSMigrationGenerator,
},
}
61 changes: 61 additions & 0 deletions packages/tax/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "@medusajs/tax",
"version": "0.1.0",
"description": "Medusa Tax module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"engines": {
"node": ">=16"
},
"bin": {
"medusa-tax-seed": "dist/scripts/bin/run-seed.js"
},
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/tax"
},
"publishConfig": {
"access": "public"
},
"author": "Medusa",
"license": "MIT",
"scripts": {
"watch": "tsc --build --watch",
"watch:test": "tsc --build tsconfig.spec.json --watch",
"prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json",
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
"test": "jest --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts",
"test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.ts",
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
"migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create",
"migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:up",
"orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
},
"devDependencies": {
"@mikro-orm/cli": "5.9.7",
"cross-env": "^5.2.1",
"jest": "^29.6.3",
"medusa-test-utils": "^1.1.40",
"rimraf": "^3.0.2",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.6",
"typescript": "^5.1.6"
},
"dependencies": {
"@medusajs/modules-sdk": "^1.12.4",
"@medusajs/types": "^1.11.8",
"@medusajs/utils": "^1.11.1",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"knex": "2.4.2"
}
}
13 changes: 13 additions & 0 deletions packages/tax/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { initializeFactory, Modules } from "@medusajs/modules-sdk"
import { moduleDefinition } from "./module-definition"

export * from "./models"
export * from "./services"

export const initialize = initializeFactory({
moduleName: Modules.TAX,
moduleDefinition,
})
export const runMigrations = moduleDefinition.runMigrations
export const revertMigration = moduleDefinition.revertMigration
export default moduleDefinition
25 changes: 25 additions & 0 deletions packages/tax/src/joiner-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"

export const LinkableKeys: Record<string, string> = {
tax_rate_id: "TaxRate",
}

const entityLinkableKeysMap: MapToConfig = {}
Object.entries(LinkableKeys).forEach(([key, value]) => {
entityLinkableKeysMap[value] ??= []
entityLinkableKeysMap[value].push({
mapTo: key,
valueFrom: key.split("_").pop()!,
})
})

export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap

export const joinerConfig: ModuleJoinerConfig = {
serviceName: Modules.TAX,
primaryKeys: ["id"],
linkableKeys: LinkableKeys,
alias: [],
} as ModuleJoinerConfig
1 change: 1 addition & 0 deletions packages/tax/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as TaxRate } from "./tax-rate"
60 changes: 60 additions & 0 deletions packages/tax/src/models/tax-rate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"

type OptionalTaxRateProps = DAL.EntityDateColumns

@Entity({ tableName: "tax_rate" })
export default class TaxRate {
[OptionalProps]: OptionalTaxRateProps

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

@Property({ columnType: "real", nullable: true })
rate: number | null = null

@Property({ columnType: "text", nullable: true })
code: string | null = null

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

@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

@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "txr")
}

@OnInit()
onInit() {
this.id = generateEntityId(this.id, "txr")
}
}
43 changes: 43 additions & 0 deletions packages/tax/src/module-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MikroOrmBaseRepository, ModulesSdkUtils } from "@medusajs/utils"
import { Modules } from "@medusajs/modules-sdk"
import { ModuleExports } from "@medusajs/types"
import * as Models from "@models"
import * as ModuleModels from "@models"
import * as ModuleServices from "@services"
import { TaxModuleService } from "@services"

const migrationScriptOptions = {
moduleName: Modules.TAX,
models: Models,
pathToMigrations: __dirname + "/migrations",
}

const runMigrations = ModulesSdkUtils.buildMigrationScript(
migrationScriptOptions
)

const revertMigration = ModulesSdkUtils.buildRevertMigrationScript(
migrationScriptOptions
)

const containerLoader = ModulesSdkUtils.moduleContainerLoaderFactory({
moduleModels: ModuleModels,
moduleRepositories: { BaseRepository: MikroOrmBaseRepository },
moduleServices: ModuleServices,
})

const connectionLoader = ModulesSdkUtils.mikroOrmConnectionLoaderFactory({
moduleName: Modules.TAX,
moduleModels: Object.values(Models),
migrationsPath: __dirname + "/migrations",
})

const service = TaxModuleService
const loaders = [containerLoader, connectionLoader] as any

export const moduleDefinition: ModuleExports = {
service,
loaders,
revertMigration,
runMigrations,
}
Loading
Loading