From cac4e3f450d7132287778f42e00c77dc55cc1085 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 16 Feb 2024 13:31:46 +0100 Subject: [PATCH 1/4] feat: tax module scaffolding --- packages/modules-sdk/src/definitions.ts | 16 ++++ packages/tax/.gitignore | 6 ++ packages/tax/README.md | 1 + .../tax/integration-tests/__tests__/index.ts | 5 ++ packages/tax/integration-tests/setup-env.js | 6 ++ packages/tax/integration-tests/setup.js | 3 + .../tax/integration-tests/utils/database.ts | 13 +++ .../utils/get-init-module-config.ts | 33 ++++++++ packages/tax/integration-tests/utils/index.ts | 2 + packages/tax/jest.config.js | 22 +++++ packages/tax/mikro-orm.config.dev.ts | 12 +++ packages/tax/package.json | 61 ++++++++++++++ packages/tax/src/index.ts | 13 +++ packages/tax/src/joiner-config.ts | 25 ++++++ packages/tax/src/models/index.ts | 1 + packages/tax/src/models/tax-rate.ts | 60 ++++++++++++++ packages/tax/src/module-definition.ts | 43 ++++++++++ packages/tax/src/scripts/bin/run-seed.ts | 29 +++++++ packages/tax/src/services/__tests__/noop.ts | 5 ++ packages/tax/src/services/index.ts | 1 + .../tax/src/services/tax-module-service.ts | 81 +++++++++++++++++++ packages/tax/tsconfig.json | 37 +++++++++ packages/tax/tsconfig.spec.json | 8 ++ packages/types/src/bundles.ts | 1 + packages/types/src/index.ts | 1 + packages/types/src/tax/common.ts | 49 +++++++++++ packages/types/src/tax/index.ts | 3 + packages/types/src/tax/mutations.ts | 7 ++ packages/types/src/tax/service.ts | 34 ++++++++ yarn.lock | 27 +++++++ 30 files changed, 605 insertions(+) create mode 100644 packages/tax/.gitignore create mode 100644 packages/tax/README.md create mode 100644 packages/tax/integration-tests/__tests__/index.ts create mode 100644 packages/tax/integration-tests/setup-env.js create mode 100644 packages/tax/integration-tests/setup.js create mode 100644 packages/tax/integration-tests/utils/database.ts create mode 100644 packages/tax/integration-tests/utils/get-init-module-config.ts create mode 100644 packages/tax/integration-tests/utils/index.ts create mode 100644 packages/tax/jest.config.js create mode 100644 packages/tax/mikro-orm.config.dev.ts create mode 100644 packages/tax/package.json create mode 100644 packages/tax/src/index.ts create mode 100644 packages/tax/src/joiner-config.ts create mode 100644 packages/tax/src/models/index.ts create mode 100644 packages/tax/src/models/tax-rate.ts create mode 100644 packages/tax/src/module-definition.ts create mode 100644 packages/tax/src/scripts/bin/run-seed.ts create mode 100644 packages/tax/src/services/__tests__/noop.ts create mode 100644 packages/tax/src/services/index.ts create mode 100644 packages/tax/src/services/tax-module-service.ts create mode 100644 packages/tax/tsconfig.json create mode 100644 packages/tax/tsconfig.spec.json create mode 100644 packages/types/src/tax/common.ts create mode 100644 packages/types/src/tax/index.ts create mode 100644 packages/types/src/tax/mutations.ts create mode 100644 packages/types/src/tax/service.ts diff --git a/packages/modules-sdk/src/definitions.ts b/packages/modules-sdk/src/definitions.ts index b3aaed2c9df04..0cd49b09a7df7 100644 --- a/packages/modules-sdk/src/definitions.ts +++ b/packages/modules-sdk/src/definitions.ts @@ -19,6 +19,7 @@ export enum Modules { PRODUCT = "productService", PROMOTION = "promotion", SALES_CHANNEL = "salesChannel", + TAX = "tax", FULFILLMENT = "fulfillment", STOCK_LOCATION = "stockLocationService", USER = "user", @@ -41,6 +42,7 @@ export enum ModuleRegistrationName { SALES_CHANNEL = "salesChannelModuleService", FULFILLMENT = "fulfillmentModuleService", STOCK_LOCATION = "stockLocationService", + TAX = "taxModuleService", USER = "userModuleService", WORKFLOW_ENGINE = "workflowsModuleService", REGION = "regionModuleService", @@ -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", @@ -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[] = diff --git a/packages/tax/.gitignore b/packages/tax/.gitignore new file mode 100644 index 0000000000000..874c6c69d3341 --- /dev/null +++ b/packages/tax/.gitignore @@ -0,0 +1,6 @@ +/dist +node_modules +.DS_store +.env* +.env +*.sql diff --git a/packages/tax/README.md b/packages/tax/README.md new file mode 100644 index 0000000000000..f502c680d54a7 --- /dev/null +++ b/packages/tax/README.md @@ -0,0 +1 @@ +# Tax Module diff --git a/packages/tax/integration-tests/__tests__/index.ts b/packages/tax/integration-tests/__tests__/index.ts new file mode 100644 index 0000000000000..333c84c1ddbf2 --- /dev/null +++ b/packages/tax/integration-tests/__tests__/index.ts @@ -0,0 +1,5 @@ +describe("noop", function () { + it("should run", function () { + expect(true).toBe(true) + }) +}) diff --git a/packages/tax/integration-tests/setup-env.js b/packages/tax/integration-tests/setup-env.js new file mode 100644 index 0000000000000..fda322997fc74 --- /dev/null +++ b/packages/tax/integration-tests/setup-env.js @@ -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" diff --git a/packages/tax/integration-tests/setup.js b/packages/tax/integration-tests/setup.js new file mode 100644 index 0000000000000..43f99aab4ac94 --- /dev/null +++ b/packages/tax/integration-tests/setup.js @@ -0,0 +1,3 @@ +import { JestUtils } from "medusa-test-utils" + +JestUtils.afterAllHookDropDatabase() diff --git a/packages/tax/integration-tests/utils/database.ts b/packages/tax/integration-tests/utils/database.ts new file mode 100644 index 0000000000000..d1ba6a830cef1 --- /dev/null +++ b/packages/tax/integration-tests/utils/database.ts @@ -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_FULFILLMENT_DB_SCHEMA +) + +export const DB_URL = TestDatabaseUtils.getDatabaseURL() diff --git a/packages/tax/integration-tests/utils/get-init-module-config.ts b/packages/tax/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000000..59e8fd4c4b497 --- /dev/null +++ b/packages/tax/integration-tests/utils/get-init-module-config.ts @@ -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: [], + } +} diff --git a/packages/tax/integration-tests/utils/index.ts b/packages/tax/integration-tests/utils/index.ts new file mode 100644 index 0000000000000..ba28fb552380b --- /dev/null +++ b/packages/tax/integration-tests/utils/index.ts @@ -0,0 +1,2 @@ +export * from "./database" +export * from "./get-init-module-config" diff --git a/packages/tax/jest.config.js b/packages/tax/jest.config.js new file mode 100644 index 0000000000000..456054fe8ae27 --- /dev/null +++ b/packages/tax/jest.config.js @@ -0,0 +1,22 @@ +module.exports = { + moduleNameMapper: { + "^@models": "/src/models", + "^@services": "/src/services", + "^@repositories": "/src/repositories", + "^@types": "/src/types", + }, + transform: { + "^.+\\.[jt]s?$": [ + "ts-jest", + { + tsConfig: "tsconfig.spec.json", + isolatedModules: true, + }, + ], + }, + testEnvironment: `node`, + moduleFileExtensions: [`js`, `ts`], + modulePathIgnorePatterns: ["dist/"], + setupFiles: ["/integration-tests/setup-env.js"], + setupFilesAfterEnv: ["/integration-tests/setup.js"], +} diff --git a/packages/tax/mikro-orm.config.dev.ts b/packages/tax/mikro-orm.config.dev.ts new file mode 100644 index 0000000000000..bd4bfe970150c --- /dev/null +++ b/packages/tax/mikro-orm.config.dev.ts @@ -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, + }, +} diff --git a/packages/tax/package.json b/packages/tax/package.json new file mode 100644 index 0000000000000..8c28cd694375f --- /dev/null +++ b/packages/tax/package.json @@ -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" + } +} diff --git a/packages/tax/src/index.ts b/packages/tax/src/index.ts new file mode 100644 index 0000000000000..4946d101141c9 --- /dev/null +++ b/packages/tax/src/index.ts @@ -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 diff --git a/packages/tax/src/joiner-config.ts b/packages/tax/src/joiner-config.ts new file mode 100644 index 0000000000000..7ffc18e24e41e --- /dev/null +++ b/packages/tax/src/joiner-config.ts @@ -0,0 +1,25 @@ +import { Modules } from "@medusajs/modules-sdk" +import { ModuleJoinerConfig } from "@medusajs/types" +import { MapToConfig } from "@medusajs/utils" + +export const LinkableKeys: Record = { + 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 diff --git a/packages/tax/src/models/index.ts b/packages/tax/src/models/index.ts new file mode 100644 index 0000000000000..65b32f2970d27 --- /dev/null +++ b/packages/tax/src/models/index.ts @@ -0,0 +1 @@ +export { default as TaxRate } from "./tax-rate" diff --git a/packages/tax/src/models/tax-rate.ts b/packages/tax/src/models/tax-rate.ts new file mode 100644 index 0000000000000..c45df9406d24d --- /dev/null +++ b/packages/tax/src/models/tax-rate.ts @@ -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 | 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") + } +} diff --git a/packages/tax/src/module-definition.ts b/packages/tax/src/module-definition.ts new file mode 100644 index 0000000000000..f574b991766d6 --- /dev/null +++ b/packages/tax/src/module-definition.ts @@ -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, +} diff --git a/packages/tax/src/scripts/bin/run-seed.ts b/packages/tax/src/scripts/bin/run-seed.ts new file mode 100644 index 0000000000000..27e8d551b9b19 --- /dev/null +++ b/packages/tax/src/scripts/bin/run-seed.ts @@ -0,0 +1,29 @@ +#!/usr/bin/env node + +import { Modules } from "@medusajs/modules-sdk" +import { ModulesSdkUtils } from "@medusajs/utils" +import * as Models from "@models" +import { EOL } from "os" + +const args = process.argv +const path = args.pop() as string + +export default (async () => { + const { config } = await import("dotenv") + config() + if (!path) { + throw new Error( + `filePath is required.${EOL}Example: medusa-tax-seed ` + ) + } + + const run = ModulesSdkUtils.buildSeedScript({ + moduleName: Modules.TAX, + models: Models, + pathToMigrations: __dirname + "/../../migrations", + seedHandler: async ({ manager, data }) => { + // TODO: Add seed logic + }, + }) + await run({ path }) +})() diff --git a/packages/tax/src/services/__tests__/noop.ts b/packages/tax/src/services/__tests__/noop.ts new file mode 100644 index 0000000000000..333c84c1ddbf2 --- /dev/null +++ b/packages/tax/src/services/__tests__/noop.ts @@ -0,0 +1,5 @@ +describe("noop", function () { + it("should run", function () { + expect(true).toBe(true) + }) +}) diff --git a/packages/tax/src/services/index.ts b/packages/tax/src/services/index.ts new file mode 100644 index 0000000000000..5f121c8a3b8d6 --- /dev/null +++ b/packages/tax/src/services/index.ts @@ -0,0 +1 @@ +export { default as TaxModuleService } from "./tax-module-service" diff --git a/packages/tax/src/services/tax-module-service.ts b/packages/tax/src/services/tax-module-service.ts new file mode 100644 index 0000000000000..fbc8a9359b2e3 --- /dev/null +++ b/packages/tax/src/services/tax-module-service.ts @@ -0,0 +1,81 @@ +import { + Context, + DAL, + ITaxRateModuleService, + InternalModuleDeclaration, + ModuleJoinerConfig, + ModulesSdkTypes, + TaxTypes, +} from "@medusajs/types" +import { + InjectManager, + InjectTransactionManager, + MedusaContext, + ModulesSdkUtils, +} from "@medusajs/utils" +import { TaxRate } from "@models" +import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config" + +type InjectedDependencies = { + baseRepository: DAL.RepositoryService + taxRateService: ModulesSdkTypes.InternalModuleService +} + +export default class TaxModuleService + extends ModulesSdkUtils.abstractModuleServiceFactory< + InjectedDependencies, + TaxTypes.TaxRateDTO, + {} + >(TaxRate, [], entityNameToLinkableKeysMap) + implements ITaxRateModuleService +{ + protected baseRepository_: DAL.RepositoryService + protected taxRateService_: ModulesSdkTypes.InternalModuleService + + constructor( + { baseRepository, taxRateService }: InjectedDependencies, + protected readonly moduleDeclaration: InternalModuleDeclaration + ) { + // @ts-ignore + super(...arguments) + + this.baseRepository_ = baseRepository + this.taxRateService_ = taxRateService + } + + __joinerConfig(): ModuleJoinerConfig { + return joinerConfig + } + + async create( + data: TaxTypes.CreateTaxRateDTO[], + sharedContext?: Context + ): Promise + + async create( + data: TaxTypes.CreateTaxRateDTO, + sharedContext?: Context + ): Promise + + @InjectManager("baseRepository_") + async create( + data: TaxTypes.CreateTaxRateDTO[] | TaxTypes.CreateTaxRateDTO, + @MedusaContext() sharedContext: Context = {} + ): Promise { + const input = Array.isArray(data) ? data : [data] + const rates = await this.create_(input, sharedContext) + const result = await this.baseRepository_.serialize( + rates, + { populate: true } + ) + return Array.isArray(data) ? result : result[0] + } + + @InjectTransactionManager("baseRepository_") + protected async create_( + data: TaxTypes.CreateTaxRateDTO[], + @MedusaContext() sharedContext: Context = {} + ) { + return await this.taxRateService_.create(data, sharedContext) + } +} diff --git a/packages/tax/tsconfig.json b/packages/tax/tsconfig.json new file mode 100644 index 0000000000000..4b79cd603235c --- /dev/null +++ b/packages/tax/tsconfig.json @@ -0,0 +1,37 @@ +{ + "compilerOptions": { + "lib": ["es2020"], + "target": "es2020", + "outDir": "./dist", + "esModuleInterop": true, + "declaration": true, + "module": "commonjs", + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "sourceMap": false, + "noImplicitReturns": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noImplicitThis": true, + "allowJs": true, + "skipLibCheck": true, + "downlevelIteration": true, // to use ES5 specific tooling + "baseUrl": ".", + "resolveJsonModule": true, + "paths": { + "@models": ["./src/models"], + "@services": ["./src/services"], + "@repositories": ["./src/repositories"], + "@types": ["./src/types"] + } + }, + "include": ["src"], + "exclude": [ + "dist", + "./src/**/__tests__", + "./src/**/__mocks__", + "./src/**/__fixtures__", + "node_modules" + ] +} diff --git a/packages/tax/tsconfig.spec.json b/packages/tax/tsconfig.spec.json new file mode 100644 index 0000000000000..48e47e8cbb3be --- /dev/null +++ b/packages/tax/tsconfig.spec.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "include": ["src", "integration-tests"], + "exclude": ["node_modules", "dist"], + "compilerOptions": { + "sourceMap": true + } +} diff --git a/packages/types/src/bundles.ts b/packages/types/src/bundles.ts index ffb500829d1f9..aaa0af865bc99 100644 --- a/packages/types/src/bundles.ts +++ b/packages/types/src/bundles.ts @@ -18,6 +18,7 @@ export * as RegionTypes from "./region__legacy" export * as SalesChannelTypes from "./sales-channel" export * as SearchTypes from "./search" export * as StockLocationTypes from "./stock-location" +export * as TaxTypes from "./tax" export * as TransactionBaseTypes from "./transaction-base" export * as UserTypes from "./user" export * as WorkflowTypes from "./workflow" diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 034b396834579..e84ba220a9825 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -27,6 +27,7 @@ export * from "./sales-channel" export * from "./search" export * from "./shared-context" export * from "./stock-location" +export * from "./tax" export * from "./totals" export * from "./transaction-base" export * from "./user" diff --git a/packages/types/src/tax/common.ts b/packages/types/src/tax/common.ts new file mode 100644 index 0000000000000..deb7c80dd8ff6 --- /dev/null +++ b/packages/types/src/tax/common.ts @@ -0,0 +1,49 @@ +import { BaseFilterable } from "../dal" +import { OperatorMap } from "../dal/utils" + +export interface TaxRateDTO { + /** + * The ID of the Tax Rate. + */ + id: string + /** + * The numerical rate to charge. + */ + rate: number | null + /** + * The code the tax rate is identified by. + */ + code: string | null + /** + * The name of the Tax Rate. E.g. "VAT". + */ + name: string + /** + * Holds custom data in key-value pairs. + */ + metadata?: Record | null + /** + * When the Tax Rate was created. + */ + created_at: string | Date + /** + * When the Tax Rate was updated. + */ + updated_at: string | Date + /** + * The ID of the user that created the Tax Rate. + */ + created_by: string +} + +export interface FilterableTaxRateProps + extends BaseFilterable { + id?: string | string[] + + rate?: number | number[] | OperatorMap + code?: string | string[] | OperatorMap + name?: string | string[] | OperatorMap + created_at?: OperatorMap + updated_at?: OperatorMap + created_by?: string | string[] | OperatorMap +} diff --git a/packages/types/src/tax/index.ts b/packages/types/src/tax/index.ts new file mode 100644 index 0000000000000..0c73656566caa --- /dev/null +++ b/packages/types/src/tax/index.ts @@ -0,0 +1,3 @@ +export * from "./common" +export * from "./mutations" +export * from "./service" diff --git a/packages/types/src/tax/mutations.ts b/packages/types/src/tax/mutations.ts new file mode 100644 index 0000000000000..9541ee7553cb3 --- /dev/null +++ b/packages/types/src/tax/mutations.ts @@ -0,0 +1,7 @@ +export interface CreateTaxRateDTO { + rate?: number | null + code?: string | null + name: string + created_by?: string + metadata?: Record +} diff --git a/packages/types/src/tax/service.ts b/packages/types/src/tax/service.ts new file mode 100644 index 0000000000000..28beeb19bfcfc --- /dev/null +++ b/packages/types/src/tax/service.ts @@ -0,0 +1,34 @@ +import { FindConfig } from "../common" +import { IModuleService } from "../modules-sdk" +import { Context } from "../shared-context" +import { FilterableTaxRateProps, TaxRateDTO } from "./common" +import { CreateTaxRateDTO } from "./mutations" + +export interface ITaxRateModuleService extends IModuleService { + retrieve( + orderId: string, + config?: FindConfig, + sharedContext?: Context + ): Promise + + list( + filters?: FilterableTaxRateProps, + config?: FindConfig, + sharedContext?: Context + ): Promise + + listAndCount( + filters?: FilterableTaxRateProps, + config?: FindConfig, + sharedContext?: Context + ): Promise<[TaxRateDTO[], number]> + + create( + data: CreateTaxRateDTO[], + sharedContext?: Context + ): Promise + create(data: CreateTaxRateDTO, sharedContext?: Context): Promise + + delete(orderIds: string[], sharedContext?: Context): Promise + delete(orderId: string, sharedContext?: Context): Promise +} diff --git a/yarn.lock b/yarn.lock index 5fdabe2bb11e2..22b410d874fb1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8703,6 +8703,33 @@ __metadata: languageName: unknown linkType: soft +"@medusajs/tax@workspace:packages/tax": + version: 0.0.0-use.local + resolution: "@medusajs/tax@workspace:packages/tax" + dependencies: + "@medusajs/modules-sdk": ^1.12.4 + "@medusajs/types": ^1.11.8 + "@medusajs/utils": ^1.11.1 + "@mikro-orm/cli": 5.9.7 + "@mikro-orm/core": 5.9.7 + "@mikro-orm/migrations": 5.9.7 + "@mikro-orm/postgresql": 5.9.7 + awilix: ^8.0.0 + cross-env: ^5.2.1 + dotenv: ^16.1.4 + jest: ^29.6.3 + knex: 2.4.2 + 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 + bin: + medusa-tax-seed: dist/scripts/bin/run-seed.js + languageName: unknown + linkType: soft + "@medusajs/toolbox@^0.0.1, @medusajs/toolbox@workspace:packages/design-system/toolbox": version: 0.0.0-use.local resolution: "@medusajs/toolbox@workspace:packages/design-system/toolbox" From 127675d1df7957682054c07f5e5e3b741eacc7ca Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 16 Feb 2024 13:33:10 +0100 Subject: [PATCH 2/4] fix: db path --- packages/order/integration-tests/utils/database.ts | 2 +- packages/tax/integration-tests/utils/database.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/order/integration-tests/utils/database.ts b/packages/order/integration-tests/utils/database.ts index d1ba6a830cef1..06c67a3bc2f9c 100644 --- a/packages/order/integration-tests/utils/database.ts +++ b/packages/order/integration-tests/utils/database.ts @@ -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() diff --git a/packages/tax/integration-tests/utils/database.ts b/packages/tax/integration-tests/utils/database.ts index d1ba6a830cef1..c3b4a6c848075 100644 --- a/packages/tax/integration-tests/utils/database.ts +++ b/packages/tax/integration-tests/utils/database.ts @@ -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_TAX_DB_SCHEMA ) export const DB_URL = TestDatabaseUtils.getDatabaseURL() From f3421f7393a405fd812051b7b04a016628b9c1a5 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 16 Feb 2024 13:36:50 +0100 Subject: [PATCH 3/4] fix: arg name --- packages/types/src/tax/service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/types/src/tax/service.ts b/packages/types/src/tax/service.ts index 28beeb19bfcfc..77ad1b8529369 100644 --- a/packages/types/src/tax/service.ts +++ b/packages/types/src/tax/service.ts @@ -6,7 +6,7 @@ import { CreateTaxRateDTO } from "./mutations" export interface ITaxRateModuleService extends IModuleService { retrieve( - orderId: string, + taxRateId: string, config?: FindConfig, sharedContext?: Context ): Promise @@ -29,6 +29,6 @@ export interface ITaxRateModuleService extends IModuleService { ): Promise create(data: CreateTaxRateDTO, sharedContext?: Context): Promise - delete(orderIds: string[], sharedContext?: Context): Promise - delete(orderId: string, sharedContext?: Context): Promise + delete(taxRateIds: string[], sharedContext?: Context): Promise + delete(taxRateId: string, sharedContext?: Context): Promise } From dee688809478e52220b05992f95e5455715de54d Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 16 Feb 2024 13:38:07 +0100 Subject: [PATCH 4/4] fix: remove ? --- packages/types/src/tax/common.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/tax/common.ts b/packages/types/src/tax/common.ts index deb7c80dd8ff6..12935161cfa1c 100644 --- a/packages/types/src/tax/common.ts +++ b/packages/types/src/tax/common.ts @@ -21,7 +21,7 @@ export interface TaxRateDTO { /** * Holds custom data in key-value pairs. */ - metadata?: Record | null + metadata: Record | null /** * When the Tax Rate was created. */