chore(): Reorganize modules (#7210)
**What** Move all modules to the modules directory
This commit is contained in:
committed by
GitHub
parent
7a351eef09
commit
4eae25e1ef
6
packages/modules/store/.gitignore
vendored
Normal file
6
packages/modules/store/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/dist
|
||||
node_modules
|
||||
.DS_store
|
||||
.env*
|
||||
.env
|
||||
*.sql
|
||||
12
packages/modules/store/CHANGELOG.md
Normal file
12
packages/modules/store/CHANGELOG.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# @medusajs/store
|
||||
|
||||
## 0.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#6700](https://github.com/medusajs/medusa/pull/6700) [`8f8a4f9b13`](https://github.com/medusajs/medusa/commit/8f8a4f9b1353087d98f6cc75346d43a7f49901a8) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Version all modules to allow for initial testing
|
||||
|
||||
- Updated dependencies [[`1fd0457c15`](https://github.com/medusajs/medusa/commit/1fd0457c153b2ef7657c052878d8e5364e1b324a), [`9288f53327`](https://github.com/medusajs/medusa/commit/9288f53327b8ce617af92ed8d14d9459cbfeb13c), [`d4b921f3db`](https://github.com/medusajs/medusa/commit/d4b921f3dbe0a38f1565a8de759996c70798d58e), [`ac86362e81`](https://github.com/medusajs/medusa/commit/ac86362e81d8523cb8e3dfad026fc94658513018), [`e4acde1aa2`](https://github.com/medusajs/medusa/commit/e4acde1aa2eb57f07e6692fe8b61f728948b9a96), [`1a661adf3e`](https://github.com/medusajs/medusa/commit/1a661adf3ef4991aa6e237dd894b6a5c47cd4aca), [`56cbf88115`](https://github.com/medusajs/medusa/commit/56cbf88115994adea7037c3f2814f0c96af3cfc0), [`36a61658f9`](https://github.com/medusajs/medusa/commit/36a61658f969a7b19c84a1e621ad1464927cafb1), [`04a532e5ef`](https://github.com/medusajs/medusa/commit/04a532e5efabbf75b1e4155520b1da175b686ffc), [`c319edb8e0`](https://github.com/medusajs/medusa/commit/c319edb8e0ecd13d086652147667916e5abab2d8), [`0b9fcb6324`](https://github.com/medusajs/medusa/commit/0b9fcb6324eee9f2556c7e6317775fae93b12a47), [`586df9da25`](https://github.com/medusajs/medusa/commit/586df9da250e492442769f5bac2f8b3de1d46f05), [`b3d826497b`](https://github.com/medusajs/medusa/commit/b3d826497b3dae5e1b26b7924706c24fd5e87ca5), [`a86c87fe14`](https://github.com/medusajs/medusa/commit/a86c87fe1442afce9285e39255914e01012b4449), [`640eccd5dd`](https://github.com/medusajs/medusa/commit/640eccd5ddbb163e0f987ce6c772f1129c2e2632), [`8ea37d03c9`](https://github.com/medusajs/medusa/commit/8ea37d03c914a5004a3e42770668b2d1f7f8f564), [`339a946f38`](https://github.com/medusajs/medusa/commit/339a946f389033c21e05338f9dbf07d88e140533), [`ac829fc67f`](https://github.com/medusajs/medusa/commit/ac829fc67f7495b08f28e55923c59f0fd6320311), [`d9d5afc3cf`](https://github.com/medusajs/medusa/commit/d9d5afc3cfc29221d0e65bff7b78474a8fb8f31f), [`c3c4f49fc2`](https://github.com/medusajs/medusa/commit/c3c4f49fc2126f950e69e291ca939ca88a15afd3), [`9288f53327`](https://github.com/medusajs/medusa/commit/9288f53327b8ce617af92ed8d14d9459cbfeb13c), [`0d46abf0ff`](https://github.com/medusajs/medusa/commit/0d46abf0ffa4c5e03bf7d2a9cdf1db828a76bea8), [`fafde4f54d`](https://github.com/medusajs/medusa/commit/fafde4f54d3ef75a7d382e6cbf94e38b3deae99b), [`8dad2b51a2`](https://github.com/medusajs/medusa/commit/8dad2b51a26c4c3c14a6c95f70424c8bef2ad63e), [`0c705d7bd4`](https://github.com/medusajs/medusa/commit/0c705d7bd41a768c48017ae95b3c8414d96c6acb), [`a6d7070dd6`](https://github.com/medusajs/medusa/commit/a6d7070dd669c21ea19d70434d42c2f8167dc309), [`1d91b7429b`](https://github.com/medusajs/medusa/commit/1d91b7429beebd6f09d5027f7f7e1fe74ce3a8ff), [`168f02f138`](https://github.com/medusajs/medusa/commit/168f02f138ad101e1013f2c8c3f8dc19de12accf), [`1ed5f918c3`](https://github.com/medusajs/medusa/commit/1ed5f918c31794a70aca4a4e4cd83cf456593baa), [`c20eb15cd9`](https://github.com/medusajs/medusa/commit/c20eb15cd9b1bd90c8d01f68eca6f0f181cd902d), [`e5945479e0`](https://github.com/medusajs/medusa/commit/e5945479e091d9560ae3e7240306a31031ef4584), [`f5c2256286`](https://github.com/medusajs/medusa/commit/f5c22562867f412040f8bc6c55ab5de3a3735e62), [`000eb61e33`](https://github.com/medusajs/medusa/commit/000eb61e33e0302db95ee6ad1656ea9b430ed471), [`d550be3685`](https://github.com/medusajs/medusa/commit/d550be3685423218d47a20c57a5e06758f4a961a), [`62a7bcc30c`](https://github.com/medusajs/medusa/commit/62a7bcc30cbc7b234b2b51d7858439951a84edeb), [`8f8a4f9b13`](https://github.com/medusajs/medusa/commit/8f8a4f9b1353087d98f6cc75346d43a7f49901a8), [`6500f18b9b`](https://github.com/medusajs/medusa/commit/6500f18b9b80c5c9c473489e7e740d55dca74303), [`ce39b9b66e`](https://github.com/medusajs/medusa/commit/ce39b9b66e8c277ec0691ea6d0a950003be09cc1), [`a6a4b3f01a`](https://github.com/medusajs/medusa/commit/a6a4b3f01a6d2bd97b1580c59134279a1b033a5d), [`4d51f095b3`](https://github.com/medusajs/medusa/commit/4d51f095b3f98f468cefb760512563f7b77bb9cf), [`4625bd1241`](https://github.com/medusajs/medusa/commit/4625bd12416275b09c22cde4a09cb0f68df5d7c1), [`56b0b45304`](https://github.com/medusajs/medusa/commit/56b0b4530401a6ec5aa155874d371e45bb388fe2), [`cc1b66842c`](https://github.com/medusajs/medusa/commit/cc1b66842cbb37c6eab84e2d8b74844c214f38d7), [`24fb102a56`](https://github.com/medusajs/medusa/commit/24fb102a564b1253d1f8b039bb1e435cc5312fbb), [`e85463b2a7`](https://github.com/medusajs/medusa/commit/e85463b2a717751de2e21c39a4c745449b31affe)]:
|
||||
- @medusajs/types@1.11.14
|
||||
- @medusajs/utils@1.11.7
|
||||
- @medusajs/modules-sdk@1.12.9
|
||||
1
packages/modules/store/README.md
Normal file
1
packages/modules/store/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# Store Module
|
||||
@@ -0,0 +1,11 @@
|
||||
import { StoreTypes } from "@medusajs/types"
|
||||
|
||||
export const createStoreFixture: StoreTypes.CreateStoreDTO = {
|
||||
name: "Test store",
|
||||
supported_currency_codes: ["usd", "eur"],
|
||||
default_sales_channel_id: "test-sales-channel",
|
||||
default_region_id: "test-region",
|
||||
metadata: {
|
||||
test: "test",
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { createStoreFixture } from "../__fixtures__"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.STORE,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IStoreModuleService>) => {
|
||||
describe("Store Module Service", () => {
|
||||
describe("creating a store", () => {
|
||||
it("should get created successfully", async function () {
|
||||
const store = await service.create(createStoreFixture)
|
||||
|
||||
expect(store).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "Test store",
|
||||
supported_currency_codes: expect.arrayContaining(["eur", "usd"]),
|
||||
default_sales_channel_id: "test-sales-channel",
|
||||
default_region_id: "test-region",
|
||||
metadata: {
|
||||
test: "test",
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it("should fail to get created if default currency code is not in list of supported currency codes", async function () {
|
||||
const err = await service
|
||||
.create({ ...createStoreFixture, default_currency_code: "jpy" })
|
||||
.catch((err) => err.message)
|
||||
|
||||
expect(err).toEqual("Store does not have currency: jpy")
|
||||
})
|
||||
|
||||
describe("upserting a store", () => {
|
||||
it("should get created if it does not exist", async function () {
|
||||
const store = await service.upsert(createStoreFixture)
|
||||
|
||||
expect(store).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "Test store",
|
||||
default_sales_channel_id: "test-sales-channel",
|
||||
default_region_id: "test-region",
|
||||
metadata: {
|
||||
test: "test",
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should get created if it does not exist", async function () {
|
||||
const createdStore = await service.upsert(createStoreFixture)
|
||||
const upsertedStore = await service.upsert({ name: "Upserted store" })
|
||||
|
||||
expect(upsertedStore).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "Upserted store",
|
||||
})
|
||||
)
|
||||
expect(upsertedStore.id).not.toEqual(createdStore.id)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updating a store", () => {
|
||||
it("should update the name successfully", async function () {
|
||||
const createdStore = await service.create(createStoreFixture)
|
||||
const updatedStore = await service.update(createdStore.id, {
|
||||
title: "Updated store",
|
||||
})
|
||||
expect(updatedStore.title).toEqual("Updated store")
|
||||
})
|
||||
|
||||
it("should fail updating default currency code to an unsupported one", async function () {
|
||||
const createdStore = await service.create(createStoreFixture)
|
||||
const updateErr = await service
|
||||
.update(createdStore.id, {
|
||||
default_currency_code: "jpy",
|
||||
})
|
||||
.catch((err) => err.message)
|
||||
|
||||
expect(updateErr).toEqual("Store does not have currency: jpy")
|
||||
})
|
||||
|
||||
it("should fail updating default currency code to an unsupported one if the supported currencies are also updated", async function () {
|
||||
const createdStore = await service.create(createStoreFixture)
|
||||
const updateErr = await service
|
||||
.update(createdStore.id, {
|
||||
supported_currency_codes: ["mkd"],
|
||||
default_currency_code: "jpy",
|
||||
})
|
||||
.catch((err) => err.message)
|
||||
|
||||
expect(updateErr).toEqual("Store does not have currency: jpy")
|
||||
})
|
||||
|
||||
it("should fail updating supported currencies if one of them is used as a default one", async function () {
|
||||
const createdStore = await service.create({
|
||||
...createStoreFixture,
|
||||
default_currency_code: "eur",
|
||||
})
|
||||
const updateErr = await service
|
||||
.update(createdStore.id, {
|
||||
supported_currency_codes: ["jpy"],
|
||||
})
|
||||
.catch((err) => err.message)
|
||||
|
||||
expect(updateErr).toEqual(
|
||||
"You are not allowed to remove default currency from store currencies without replacing it as well"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleting a store", () => {
|
||||
it("should successfully delete existing stores", async function () {
|
||||
const createdStore = await service.create([
|
||||
createStoreFixture,
|
||||
createStoreFixture,
|
||||
])
|
||||
|
||||
await service.delete([createdStore[0].id, createdStore[1].id])
|
||||
|
||||
const storeInDatabase = await service.list()
|
||||
expect(storeInDatabase).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieving a store", () => {
|
||||
it("should successfully return all existing stores", async function () {
|
||||
await service.create([
|
||||
createStoreFixture,
|
||||
{ ...createStoreFixture, name: "Another store" },
|
||||
])
|
||||
|
||||
const storesInDatabase = await service.list()
|
||||
expect(storesInDatabase).toHaveLength(2)
|
||||
expect(storesInDatabase.map((s) => s.name)).toEqual(
|
||||
expect.arrayContaining(["Test store", "Another store"])
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
20
packages/modules/store/jest.config.js
Normal file
20
packages/modules/store/jest.config.js
Normal file
@@ -0,0 +1,20 @@
|
||||
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/"],
|
||||
}
|
||||
8
packages/modules/store/mikro-orm.config.dev.ts
Normal file
8
packages/modules/store/mikro-orm.config.dev.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as entities from "./src/models"
|
||||
|
||||
module.exports = {
|
||||
entities: Object.values(entities),
|
||||
schema: "public",
|
||||
clientUrl: "postgres://postgres@localhost/medusa-store",
|
||||
type: "postgresql",
|
||||
}
|
||||
61
packages/modules/store/package.json
Normal file
61
packages/modules/store/package.json
Normal file
@@ -0,0 +1,61 @@
|
||||
{
|
||||
"name": "@medusajs/store",
|
||||
"version": "0.1.1",
|
||||
"description": "Medusa Store module",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-store-seed": "dist/scripts/bin/run-seed.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/medusajs/medusa",
|
||||
"directory": "packages/store"
|
||||
},
|
||||
"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 --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": "workspace:^",
|
||||
"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.11",
|
||||
"@medusajs/types": "^1.11.16",
|
||||
"@medusajs/utils": "^1.11.9",
|
||||
"@mikro-orm/core": "5.9.7",
|
||||
"@mikro-orm/migrations": "5.9.7",
|
||||
"@mikro-orm/postgresql": "5.9.7",
|
||||
"awilix": "^8.0.0",
|
||||
"dotenv": "^16.4.5",
|
||||
"knex": "2.4.2"
|
||||
}
|
||||
}
|
||||
14
packages/modules/store/src/index.ts
Normal file
14
packages/modules/store/src/index.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { initializeFactory, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
export * from "./types"
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
|
||||
export const initialize = initializeFactory({
|
||||
moduleName: Modules.STORE,
|
||||
moduleDefinition,
|
||||
})
|
||||
export const runMigrations = moduleDefinition.runMigrations
|
||||
export const revertMigration = moduleDefinition.revertMigration
|
||||
export default moduleDefinition
|
||||
29
packages/modules/store/src/joiner-config.ts
Normal file
29
packages/modules/store/src/joiner-config.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import Store from "./models/store"
|
||||
|
||||
export const LinkableKeys: Record<string, string> = {}
|
||||
|
||||
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.STORE,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["store", "stores"],
|
||||
args: { entity: Store.name },
|
||||
},
|
||||
],
|
||||
} as ModuleJoinerConfig
|
||||
@@ -0,0 +1,143 @@
|
||||
{
|
||||
"namespaces": [
|
||||
"public"
|
||||
],
|
||||
"name": "public",
|
||||
"tables": [
|
||||
{
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"mappedType": "text"
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"default": "'Medusa Store'",
|
||||
"mappedType": "text"
|
||||
},
|
||||
"supported_currency_codes": {
|
||||
"name": "supported_currency_codes",
|
||||
"type": "text[]",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"default": "'{}'",
|
||||
"mappedType": "array"
|
||||
},
|
||||
"default_currency_code": {
|
||||
"name": "default_currency_code",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "text"
|
||||
},
|
||||
"default_sales_channel_id": {
|
||||
"name": "default_sales_channel_id",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "text"
|
||||
},
|
||||
"default_region_id": {
|
||||
"name": "default_region_id",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "text"
|
||||
},
|
||||
"default_location_id": {
|
||||
"name": "default_location_id",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "text"
|
||||
},
|
||||
"metadata": {
|
||||
"name": "metadata",
|
||||
"type": "jsonb",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "json"
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamptz",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"length": 6,
|
||||
"default": "now()",
|
||||
"mappedType": "datetime"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamptz",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"length": 6,
|
||||
"default": "now()",
|
||||
"mappedType": "datetime"
|
||||
},
|
||||
"deleted_at": {
|
||||
"name": "deleted_at",
|
||||
"type": "timestamptz",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"length": 6,
|
||||
"mappedType": "datetime"
|
||||
}
|
||||
},
|
||||
"name": "store",
|
||||
"schema": "public",
|
||||
"indexes": [
|
||||
{
|
||||
"keyName": "IDX_store_deleted_at",
|
||||
"columnNames": [
|
||||
"deleted_at"
|
||||
],
|
||||
"composite": false,
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_store_deleted_at\" ON \"store\" (deleted_at) WHERE deleted_at IS NOT NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "store_pkey",
|
||||
"columnNames": [
|
||||
"id"
|
||||
],
|
||||
"composite": false,
|
||||
"primary": true,
|
||||
"unique": true
|
||||
}
|
||||
],
|
||||
"checks": [],
|
||||
"foreignKeys": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { Migration } from "@mikro-orm/migrations"
|
||||
|
||||
export class InitialSetup20240226130829 extends Migration {
|
||||
async up(): Promise<void> {
|
||||
// TODO: The migration needs to take care of moving data before dropping columns, among other things
|
||||
const storeTables = await this.execute(
|
||||
"select * from information_schema.tables where table_name = 'store' and table_schema = 'public'"
|
||||
)
|
||||
|
||||
if (storeTables.length > 0) {
|
||||
this.addSql(`alter table "store" alter column "id" TYPE text;`)
|
||||
this.addSql(`alter table "store" alter column "name" TYPE text;`)
|
||||
this.addSql(
|
||||
`alter table "store" alter column "name" SET DEFAULT 'Medusa Store';`
|
||||
)
|
||||
this.addSql(
|
||||
`alter table "store" alter column "default_currency_code" TYPE text;`
|
||||
)
|
||||
this.addSql(
|
||||
`alter table "store" alter column "default_currency_code" drop not null;`
|
||||
)
|
||||
this.addSql(
|
||||
`alter table "store" alter column "default_sales_channel_id" TYPE text;`
|
||||
)
|
||||
this.addSql(
|
||||
`alter table "store" alter column "default_location_id" TYPE text;`
|
||||
)
|
||||
|
||||
this.addSql(
|
||||
`alter table "store" add column "default_region_id" text null;`
|
||||
)
|
||||
this.addSql(
|
||||
`alter table "store" add column "deleted_at" timestamptz null;`
|
||||
)
|
||||
this.addSql(
|
||||
`alter table "store" add column "supported_currency_codes" text[] not null default \'{}\';`
|
||||
)
|
||||
|
||||
this.addSql(
|
||||
'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;'
|
||||
)
|
||||
|
||||
this.addSql(
|
||||
`alter table "store" drop constraint if exists "FK_61b0f48cccbb5f41c750bac7286";`
|
||||
)
|
||||
this.addSql(
|
||||
`alter table "store" drop constraint if exists "FK_55beebaa09e947cccca554af222";`
|
||||
)
|
||||
|
||||
// this.addSql(`alter table "store" drop column "default_currency_code";`)
|
||||
// this.addSql(`alter table "store" drop column "swap_link_template";`)
|
||||
// this.addSql(`alter table "store" drop column "payment_link_template";`)
|
||||
// this.addSql(`alter table "store" drop column "invite_link_template";`)
|
||||
} else {
|
||||
this.addSql(`create table if not exists "store"
|
||||
("id" text not null, "name" text not null default \'Medusa Store\', "supported_currency_codes" text[] not null default \'{}\',
|
||||
"default_currency_code" text null, "default_sales_channel_id" text null, "default_region_id" text null, "default_location_id" text null,
|
||||
"metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null,
|
||||
constraint "store_pkey" primary key ("id"));`)
|
||||
|
||||
this.addSql(
|
||||
'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;'
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
1
packages/modules/store/src/models/index.ts
Normal file
1
packages/modules/store/src/models/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as Store } from "./store"
|
||||
86
packages/modules/store/src/models/store.ts
Normal file
86
packages/modules/store/src/models/store.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import {
|
||||
DALUtils,
|
||||
Searchable,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { DAL } from "@medusajs/types"
|
||||
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
OnInit,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Filter,
|
||||
OptionalProps,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
type StoreOptionalProps = DAL.SoftDeletableEntityDateColumns
|
||||
|
||||
const StoreDeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "store",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
@Entity()
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class Store {
|
||||
[OptionalProps]?: StoreOptionalProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", default: "Medusa Store" })
|
||||
name: string
|
||||
|
||||
@Property({ type: "array", default: "{}" })
|
||||
supported_currency_codes: string[] = []
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
default_currency_code: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
default_sales_channel_id: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
default_region_id: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
default_location_id: string | null = null
|
||||
|
||||
@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
|
||||
|
||||
@StoreDeletedAtIndex.MikroORMIndex()
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "store")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "store")
|
||||
}
|
||||
}
|
||||
44
packages/modules/store/src/module-definition.ts
Normal file
44
packages/modules/store/src/module-definition.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import * as ModuleServices from "@services"
|
||||
import { StoreModuleService } from "@services"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import * as Models from "@models"
|
||||
import * as ModuleModels from "@models"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import * as ModuleRepositories from "@repositories"
|
||||
|
||||
const migrationScriptOptions = {
|
||||
moduleName: Modules.STORE,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/migrations",
|
||||
}
|
||||
|
||||
const runMigrations = ModulesSdkUtils.buildMigrationScript(
|
||||
migrationScriptOptions
|
||||
)
|
||||
|
||||
const revertMigration = ModulesSdkUtils.buildRevertMigrationScript(
|
||||
migrationScriptOptions
|
||||
)
|
||||
|
||||
const containerLoader = ModulesSdkUtils.moduleContainerLoaderFactory({
|
||||
moduleModels: ModuleModels,
|
||||
moduleRepositories: ModuleRepositories,
|
||||
moduleServices: ModuleServices,
|
||||
})
|
||||
|
||||
const connectionLoader = ModulesSdkUtils.mikroOrmConnectionLoaderFactory({
|
||||
moduleName: Modules.STORE,
|
||||
moduleModels: Object.values(Models),
|
||||
migrationsPath: __dirname + "/migrations",
|
||||
})
|
||||
|
||||
const service = StoreModuleService
|
||||
const loaders = [containerLoader, connectionLoader] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
revertMigration,
|
||||
runMigrations,
|
||||
}
|
||||
1
packages/modules/store/src/repositories/index.ts
Normal file
1
packages/modules/store/src/repositories/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
29
packages/modules/store/src/scripts/bin/run-seed.ts
Normal file
29
packages/modules/store/src/scripts/bin/run-seed.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
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-store-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
const run = ModulesSdkUtils.buildSeedScript({
|
||||
moduleName: Modules.STORE,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/../../migrations",
|
||||
seedHandler: async ({ manager, data }) => {
|
||||
// TODO: Add seed logic
|
||||
},
|
||||
})
|
||||
await run({ path })
|
||||
})()
|
||||
5
packages/modules/store/src/services/__tests__/noop.ts
Normal file
5
packages/modules/store/src/services/__tests__/noop.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
describe("noop", function () {
|
||||
it("should run", function () {
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
})
|
||||
1
packages/modules/store/src/services/index.ts
Normal file
1
packages/modules/store/src/services/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as StoreModuleService } from "./store-module-service"
|
||||
265
packages/modules/store/src/services/store-module-service.ts
Normal file
265
packages/modules/store/src/services/store-module-service.ts
Normal file
@@ -0,0 +1,265 @@
|
||||
import {
|
||||
DAL,
|
||||
InternalModuleDeclaration,
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
IStoreModuleService,
|
||||
StoreTypes,
|
||||
Context,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
isString,
|
||||
promiseAll,
|
||||
removeUndefined,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { Store } from "@models"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
import { UpdateStoreInput } from "@types"
|
||||
|
||||
const generateMethodForModels = []
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
storeService: ModulesSdkTypes.InternalModuleService<any>
|
||||
}
|
||||
|
||||
export default class StoreModuleService<TEntity extends Store = Store>
|
||||
extends ModulesSdkUtils.abstractModuleServiceFactory<
|
||||
InjectedDependencies,
|
||||
StoreTypes.StoreDTO,
|
||||
{
|
||||
Store: { dto: StoreTypes.StoreDTO }
|
||||
}
|
||||
>(Store, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements IStoreModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly storeService_: ModulesSdkTypes.InternalModuleService<TEntity>
|
||||
|
||||
constructor(
|
||||
{ baseRepository, storeService }: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
this.baseRepository_ = baseRepository
|
||||
this.storeService_ = storeService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
async create(
|
||||
data: StoreTypes.CreateStoreDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<StoreTypes.StoreDTO[]>
|
||||
async create(
|
||||
data: StoreTypes.CreateStoreDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<StoreTypes.StoreDTO>
|
||||
@InjectManager("baseRepository_")
|
||||
async create(
|
||||
data: StoreTypes.CreateStoreDTO | StoreTypes.CreateStoreDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const result = await this.create_(input, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<StoreTypes.StoreDTO[]>(
|
||||
Array.isArray(data) ? result : result[0]
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async create_(
|
||||
data: StoreTypes.CreateStoreDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<Store[]> {
|
||||
let normalizedInput = StoreModuleService.normalizeInput(data)
|
||||
StoreModuleService.validateCreateRequest(normalizedInput)
|
||||
|
||||
return await this.storeService_.create(normalizedInput, sharedContext)
|
||||
}
|
||||
|
||||
async upsert(
|
||||
data: StoreTypes.UpsertStoreDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<StoreTypes.StoreDTO[]>
|
||||
async upsert(
|
||||
data: StoreTypes.UpsertStoreDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<StoreTypes.StoreDTO>
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async upsert(
|
||||
data: StoreTypes.UpsertStoreDTO | StoreTypes.UpsertStoreDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const forUpdate = input.filter(
|
||||
(store): store is UpdateStoreInput => !!store.id
|
||||
)
|
||||
const forCreate = input.filter(
|
||||
(store): store is StoreTypes.CreateStoreDTO => !store.id
|
||||
)
|
||||
|
||||
const operations: Promise<Store[]>[] = []
|
||||
|
||||
if (forCreate.length) {
|
||||
operations.push(this.create_(forCreate, sharedContext))
|
||||
}
|
||||
if (forUpdate.length) {
|
||||
operations.push(this.update_(forUpdate, sharedContext))
|
||||
}
|
||||
|
||||
const result = (await promiseAll(operations)).flat()
|
||||
return await this.baseRepository_.serialize<
|
||||
StoreTypes.StoreDTO[] | StoreTypes.StoreDTO
|
||||
>(Array.isArray(data) ? result : result[0])
|
||||
}
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
data: StoreTypes.UpdateStoreDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<StoreTypes.StoreDTO>
|
||||
async update(
|
||||
selector: StoreTypes.FilterableStoreProps,
|
||||
data: StoreTypes.UpdateStoreDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<StoreTypes.StoreDTO[]>
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
idOrSelector: string | StoreTypes.FilterableStoreProps,
|
||||
data: StoreTypes.UpdateStoreDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
|
||||
let normalizedInput: UpdateStoreInput[] = []
|
||||
if (isString(idOrSelector)) {
|
||||
normalizedInput = [{ id: idOrSelector, ...data }]
|
||||
} else {
|
||||
const stores = await this.storeService_.list(
|
||||
idOrSelector,
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
normalizedInput = stores.map((store) => ({
|
||||
id: store.id,
|
||||
...data,
|
||||
}))
|
||||
}
|
||||
|
||||
const updateResult = await this.update_(normalizedInput, sharedContext)
|
||||
|
||||
const stores = await this.baseRepository_.serialize<
|
||||
StoreTypes.StoreDTO[] | StoreTypes.StoreDTO
|
||||
>(updateResult)
|
||||
|
||||
return isString(idOrSelector) ? stores[0] : stores
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
data: UpdateStoreInput[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<Store[]> {
|
||||
const normalizedInput = StoreModuleService.normalizeInput(data)
|
||||
await this.validateUpdateRequest(normalizedInput)
|
||||
return await this.storeService_.update(normalizedInput, sharedContext)
|
||||
}
|
||||
|
||||
private static normalizeInput<T extends StoreTypes.UpdateStoreDTO>(
|
||||
stores: T[]
|
||||
): T[] {
|
||||
return stores.map((store) =>
|
||||
removeUndefined({
|
||||
...store,
|
||||
name: store.name?.trim(),
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
private static validateCreateRequest(stores: StoreTypes.CreateStoreDTO[]) {
|
||||
for (const store of stores) {
|
||||
// If we are setting the default currency code on creating, make sure it is supported
|
||||
if (store.default_currency_code) {
|
||||
if (
|
||||
!store.supported_currency_codes?.includes(
|
||||
store.default_currency_code ?? ""
|
||||
)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Store does not have currency: ${store.default_currency_code}`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async validateUpdateRequest(stores: UpdateStoreInput[]) {
|
||||
const dbStores = await this.storeService_.list(
|
||||
{ id: stores.map((s) => s.id) },
|
||||
{ take: null }
|
||||
)
|
||||
|
||||
const dbStoresMap = new Map<string, Store>(
|
||||
dbStores.map((dbStore) => [dbStore.id, dbStore])
|
||||
)
|
||||
|
||||
for (const store of stores) {
|
||||
const dbStore = dbStoresMap.get(store.id)
|
||||
|
||||
// If it is updating both the supported currency codes and the default one, look in that list
|
||||
if (store.supported_currency_codes && store.default_currency_code) {
|
||||
if (
|
||||
!store.supported_currency_codes.includes(
|
||||
store.default_currency_code ?? ""
|
||||
)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Store does not have currency: ${store.default_currency_code}`
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// If it is updating only the default currency code, look in the db store
|
||||
if (store.default_currency_code) {
|
||||
if (
|
||||
!dbStore?.supported_currency_codes?.includes(
|
||||
store.default_currency_code
|
||||
)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Store does not have currency: ${store.default_currency_code}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// If it is updating only the supported currency codes, make sure one of them is not set as a default one
|
||||
if (store.supported_currency_codes) {
|
||||
if (
|
||||
!store.supported_currency_codes.includes(
|
||||
dbStore?.default_currency_code ?? ""
|
||||
)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"You are not allowed to remove default currency from store currencies without replacing it as well"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
packages/modules/store/src/types/index.ts
Normal file
9
packages/modules/store/src/types/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { StoreTypes } from "@medusajs/types"
|
||||
import { IEventBusModuleService, Logger } from "@medusajs/types"
|
||||
|
||||
export type InitializeModuleInjectableDependencies = {
|
||||
logger?: Logger
|
||||
eventBusService?: IEventBusModuleService
|
||||
}
|
||||
|
||||
export type UpdateStoreInput = StoreTypes.UpdateStoreDTO & { id: string }
|
||||
37
packages/modules/store/tsconfig.json
Normal file
37
packages/modules/store/tsconfig.json
Normal file
@@ -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"
|
||||
]
|
||||
}
|
||||
8
packages/modules/store/tsconfig.spec.json
Normal file
8
packages/modules/store/tsconfig.spec.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["src", "integration-tests"],
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"compilerOptions": {
|
||||
"sourceMap": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user