@medusajs/store 0.1.2-snapshot-20240718073308 → 0.1.2-snapshot-20240718083016

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- import { StoreModuleService } from "./services";
2
- declare const _default: import("@medusajs/types").ModuleExports<typeof StoreModuleService> & {
3
- linkable: Record<string, any>;
4
- };
5
- export default _default;
6
- //# sourceMappingURL=index.d.ts.map
1
+ import { moduleDefinition } from "./module-definition";
2
+ export * from "./types";
3
+ export * from "./models";
4
+ export * from "./services";
5
+ export declare const initialize: (options?: import("@medusajs/modules-sdk").InternalModuleDeclaration | import("@medusajs/modules-sdk").ModuleServiceInitializeOptions | import("@medusajs/modules-sdk").ModuleServiceInitializeCustomDataLayerOptions | import("@medusajs/modules-sdk").ExternalModuleDeclaration | undefined, injectedDependencies?: any) => Promise<unknown>;
6
+ export declare const runMigrations: ((options: import("@medusajs/modules-sdk").LoaderOptions<any>, moduleDeclaration?: import("@medusajs/modules-sdk").InternalModuleDeclaration | undefined) => Promise<void>) | undefined;
7
+ export declare const revertMigration: ((options: import("@medusajs/modules-sdk").LoaderOptions<any>, moduleDeclaration?: import("@medusajs/modules-sdk").InternalModuleDeclaration | undefined) => Promise<void>) | undefined;
8
+ export default moduleDefinition;
package/dist/index.js CHANGED
@@ -1,7 +1,29 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const _services_1 = require("./services");
4
- const utils_1 = require("@medusajs/utils");
5
- exports.default = (0, utils_1.Module)(utils_1.Modules.STORE, {
6
- service: _services_1.StoreModuleService,
17
+ exports.revertMigration = exports.runMigrations = exports.initialize = void 0;
18
+ const module_definition_1 = require("./module-definition");
19
+ const modules_sdk_1 = require("@medusajs/modules-sdk");
20
+ __exportStar(require("./types"), exports);
21
+ __exportStar(require("./models"), exports);
22
+ __exportStar(require("./services"), exports);
23
+ exports.initialize = (0, modules_sdk_1.initializeFactory)({
24
+ moduleName: modules_sdk_1.Modules.STORE,
25
+ moduleDefinition: module_definition_1.moduleDefinition,
7
26
  });
27
+ exports.runMigrations = module_definition_1.moduleDefinition.runMigrations;
28
+ exports.revertMigration = module_definition_1.moduleDefinition.revertMigration;
29
+ exports.default = module_definition_1.moduleDefinition;
@@ -0,0 +1,5 @@
1
+ import { ModuleJoinerConfig } from "@medusajs/types";
2
+ import { MapToConfig } from "@medusajs/utils";
3
+ export declare const LinkableKeys: Record<string, string>;
4
+ export declare const entityNameToLinkableKeysMap: MapToConfig;
5
+ export declare const joinerConfig: ModuleJoinerConfig;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.joinerConfig = exports.entityNameToLinkableKeysMap = exports.LinkableKeys = void 0;
7
+ const modules_sdk_1 = require("@medusajs/modules-sdk");
8
+ const store_1 = __importDefault(require("./models/store"));
9
+ exports.LinkableKeys = {};
10
+ const entityLinkableKeysMap = {};
11
+ Object.entries(exports.LinkableKeys).forEach(([key, value]) => {
12
+ entityLinkableKeysMap[value] ?? (entityLinkableKeysMap[value] = []);
13
+ entityLinkableKeysMap[value].push({
14
+ mapTo: key,
15
+ valueFrom: key.split("_").pop(),
16
+ });
17
+ });
18
+ exports.entityNameToLinkableKeysMap = entityLinkableKeysMap;
19
+ exports.joinerConfig = {
20
+ serviceName: modules_sdk_1.Modules.STORE,
21
+ primaryKeys: ["id"],
22
+ linkableKeys: exports.LinkableKeys,
23
+ alias: [
24
+ {
25
+ name: ["store", "stores"],
26
+ args: { entity: store_1.default.name },
27
+ },
28
+ ],
29
+ };
@@ -2,4 +2,3 @@ import { Migration } from "@mikro-orm/migrations";
2
2
  export declare class InitialSetup20240226130829 extends Migration {
3
3
  up(): Promise<void>;
4
4
  }
5
- //# sourceMappingURL=InitialSetup20240227075933.d.ts.map
@@ -1,3 +1 @@
1
1
  export { default as Store } from "./store";
2
- export { default as StoreCurrency } from "./currency";
3
- //# sourceMappingURL=index.d.ts.map
@@ -3,8 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.StoreCurrency = exports.Store = void 0;
6
+ exports.Store = void 0;
7
7
  var store_1 = require("./store");
8
8
  Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return __importDefault(store_1).default; } });
9
- var currency_1 = require("./currency");
10
- Object.defineProperty(exports, "StoreCurrency", { enumerable: true, get: function () { return __importDefault(currency_1).default; } });
@@ -1,12 +1,12 @@
1
1
  import { DAL } from "@medusajs/types";
2
- import { OptionalProps, Collection } from "@mikro-orm/core";
3
- import StoreCurrency from "./currency";
4
- type StoreOptionalProps = DAL.SoftDeletableModelDateColumns;
2
+ import { OptionalProps } from "@mikro-orm/core";
3
+ type StoreOptionalProps = DAL.SoftDeletableEntityDateColumns;
5
4
  export default class Store {
6
5
  [OptionalProps]?: StoreOptionalProps;
7
6
  id: string;
8
7
  name: string;
9
- supported_currencies: Collection<StoreCurrency, object>;
8
+ supported_currency_codes: string[];
9
+ default_currency_code: string | null;
10
10
  default_sales_channel_id: string | null;
11
11
  default_region_id: string | null;
12
12
  default_location_id: string | null;
@@ -18,4 +18,3 @@ export default class Store {
18
18
  onInit(): void;
19
19
  }
20
20
  export {};
21
- //# sourceMappingURL=store.d.ts.map
@@ -8,13 +8,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  const utils_1 = require("@medusajs/utils");
16
13
  const core_1 = require("@mikro-orm/core");
17
- const currency_1 = __importDefault(require("./currency"));
18
14
  const StoreDeletedAtIndex = (0, utils_1.createPsqlIndexStatementHelper)({
19
15
  tableName: "store",
20
16
  columns: "deleted_at",
@@ -22,7 +18,8 @@ const StoreDeletedAtIndex = (0, utils_1.createPsqlIndexStatementHelper)({
22
18
  });
23
19
  let Store = class Store {
24
20
  constructor() {
25
- this.supported_currencies = new core_1.Collection(this);
21
+ this.supported_currency_codes = [];
22
+ this.default_currency_code = null;
26
23
  this.default_sales_channel_id = null;
27
24
  this.default_region_id = null;
28
25
  this.default_location_id = null;
@@ -46,11 +43,13 @@ __decorate([
46
43
  __metadata("design:type", String)
47
44
  ], Store.prototype, "name", void 0);
48
45
  __decorate([
49
- (0, core_1.OneToMany)(() => currency_1.default, (o) => o.store, {
50
- cascade: [core_1.Cascade.PERSIST, "soft-remove"],
51
- }),
46
+ (0, core_1.Property)({ type: "array", default: "{}" }),
47
+ __metadata("design:type", Array)
48
+ ], Store.prototype, "supported_currency_codes", void 0);
49
+ __decorate([
50
+ (0, core_1.Property)({ columnType: "text", nullable: true }),
52
51
  __metadata("design:type", Object)
53
- ], Store.prototype, "supported_currencies", void 0);
52
+ ], Store.prototype, "default_currency_code", void 0);
54
53
  __decorate([
55
54
  (0, core_1.Property)({ columnType: "text", nullable: true }),
56
55
  __metadata("design:type", Object)
@@ -0,0 +1,2 @@
1
+ import { ModuleExports } from "@medusajs/types";
2
+ export declare const moduleDefinition: ModuleExports;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.moduleDefinition = void 0;
27
+ const ModuleServices = __importStar(require("./services"));
28
+ const _services_1 = require("./services");
29
+ const modules_sdk_1 = require("@medusajs/modules-sdk");
30
+ const Models = __importStar(require("./models"));
31
+ const ModuleModels = __importStar(require("./models"));
32
+ const utils_1 = require("@medusajs/utils");
33
+ const ModuleRepositories = __importStar(require("./repositories"));
34
+ const migrationScriptOptions = {
35
+ moduleName: modules_sdk_1.Modules.STORE,
36
+ models: Models,
37
+ pathToMigrations: __dirname + "/migrations",
38
+ };
39
+ const runMigrations = utils_1.ModulesSdkUtils.buildMigrationScript(migrationScriptOptions);
40
+ const revertMigration = utils_1.ModulesSdkUtils.buildRevertMigrationScript(migrationScriptOptions);
41
+ const containerLoader = utils_1.ModulesSdkUtils.moduleContainerLoaderFactory({
42
+ moduleModels: ModuleModels,
43
+ moduleRepositories: ModuleRepositories,
44
+ moduleServices: ModuleServices,
45
+ });
46
+ const connectionLoader = utils_1.ModulesSdkUtils.mikroOrmConnectionLoaderFactory({
47
+ moduleName: modules_sdk_1.Modules.STORE,
48
+ moduleModels: Object.values(Models),
49
+ migrationsPath: __dirname + "/migrations",
50
+ });
51
+ const service = _services_1.StoreModuleService;
52
+ const loaders = [containerLoader, connectionLoader];
53
+ exports.moduleDefinition = {
54
+ service,
55
+ loaders,
56
+ revertMigration,
57
+ runMigrations,
58
+ };
@@ -0,0 +1 @@
1
+ export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseRepository = void 0;
4
+ var utils_1 = require("@medusajs/utils");
5
+ Object.defineProperty(exports, "BaseRepository", { enumerable: true, get: function () { return utils_1.MikroOrmBaseRepository; } });
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ declare const _default: Promise<void>;
3
+ export default _default;
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ const utils_1 = require("@medusajs/utils");
28
+ const modules_sdk_1 = require("@medusajs/modules-sdk");
29
+ const Models = __importStar(require("../../models"));
30
+ const os_1 = require("os");
31
+ const args = process.argv;
32
+ const path = args.pop();
33
+ exports.default = (async () => {
34
+ const { config } = await Promise.resolve().then(() => __importStar(require("dotenv")));
35
+ config();
36
+ if (!path) {
37
+ throw new Error(`filePath is required.${os_1.EOL}Example: medusa-store-seed <filePath>`);
38
+ }
39
+ const run = utils_1.ModulesSdkUtils.buildSeedScript({
40
+ moduleName: modules_sdk_1.Modules.STORE,
41
+ models: Models,
42
+ pathToMigrations: __dirname + "/../../migrations",
43
+ seedHandler: async ({ manager, data }) => {
44
+ // TODO: Add seed logic
45
+ },
46
+ });
47
+ await run({ path });
48
+ })();
@@ -1,2 +1 @@
1
1
  export { default as StoreModuleService } from "./store-module-service";
2
- //# sourceMappingURL=index.d.ts.map
@@ -1,34 +1,32 @@
1
- import { Context, DAL, InternalModuleDeclaration, IStoreModuleService, ModulesSdkTypes, StoreTypes } from "@medusajs/types";
1
+ import { DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, IStoreModuleService, StoreTypes, Context } from "@medusajs/types";
2
+ import { ModulesSdkUtils } from "@medusajs/utils";
2
3
  import { Store } from "../models";
3
4
  import { UpdateStoreInput } from "../types";
4
5
  type InjectedDependencies = {
5
6
  baseRepository: DAL.RepositoryService;
6
- storeService: ModulesSdkTypes.IMedusaInternalService<any>;
7
+ storeService: ModulesSdkTypes.InternalModuleService<any>;
7
8
  };
8
- declare const StoreModuleService_base: import("@medusajs/utils/dist/modules-sdk/types/medusa-service").MedusaServiceReturnType<{
9
+ declare const StoreModuleService_base: new (container: InjectedDependencies) => ModulesSdkUtils.AbstractModuleService<InjectedDependencies, StoreTypes.StoreDTO, {
9
10
  Store: {
10
11
  dto: StoreTypes.StoreDTO;
11
12
  };
12
- StoreCurrency: {
13
- dto: StoreTypes.StoreCurrencyDTO;
14
- };
15
13
  }>;
16
- export default class StoreModuleService extends StoreModuleService_base implements IStoreModuleService {
14
+ export default class StoreModuleService<TEntity extends Store = Store> extends StoreModuleService_base implements IStoreModuleService {
17
15
  protected readonly moduleDeclaration: InternalModuleDeclaration;
18
16
  protected baseRepository_: DAL.RepositoryService;
19
- protected readonly storeService_: ModulesSdkTypes.IMedusaInternalService<Store>;
17
+ protected readonly storeService_: ModulesSdkTypes.InternalModuleService<TEntity>;
20
18
  constructor({ baseRepository, storeService }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
21
- createStores(data: StoreTypes.CreateStoreDTO[], sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
22
- createStores(data: StoreTypes.CreateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
19
+ __joinerConfig(): ModuleJoinerConfig;
20
+ create(data: StoreTypes.CreateStoreDTO[], sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
21
+ create(data: StoreTypes.CreateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
23
22
  create_(data: StoreTypes.CreateStoreDTO[], sharedContext?: Context): Promise<Store[]>;
24
- upsertStores(data: StoreTypes.UpsertStoreDTO[], sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
25
- upsertStores(data: StoreTypes.UpsertStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
26
- updateStores(id: string, data: StoreTypes.UpdateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
27
- updateStores(selector: StoreTypes.FilterableStoreProps, data: StoreTypes.UpdateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
23
+ upsert(data: StoreTypes.UpsertStoreDTO[], sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
24
+ upsert(data: StoreTypes.UpsertStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
25
+ update(id: string, data: StoreTypes.UpdateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
26
+ update(selector: StoreTypes.FilterableStoreProps, data: StoreTypes.UpdateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
28
27
  protected update_(data: UpdateStoreInput[], sharedContext?: Context): Promise<Store[]>;
29
28
  private static normalizeInput;
30
29
  private static validateCreateRequest;
31
- private static validateUpdateRequest;
30
+ private validateUpdateRequest;
32
31
  }
33
32
  export {};
34
- //# sourceMappingURL=store-module-service.d.ts.map
@@ -15,7 +15,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const types_1 = require("@medusajs/types");
16
16
  const utils_1 = require("@medusajs/utils");
17
17
  const _models_1 = require("../models");
18
- class StoreModuleService extends (0, utils_1.MedusaService)({ Store: _models_1.Store, StoreCurrency: _models_1.StoreCurrency }) {
18
+ const joiner_config_1 = require("../joiner-config");
19
+ const generateMethodForModels = [];
20
+ class StoreModuleService extends utils_1.ModulesSdkUtils.abstractModuleServiceFactory(_models_1.Store, generateMethodForModels, joiner_config_1.entityNameToLinkableKeysMap) {
19
21
  constructor({ baseRepository, storeService }, moduleDeclaration) {
20
22
  // @ts-ignore
21
23
  super(...arguments);
@@ -23,7 +25,10 @@ class StoreModuleService extends (0, utils_1.MedusaService)({ Store: _models_1.S
23
25
  this.baseRepository_ = baseRepository;
24
26
  this.storeService_ = storeService;
25
27
  }
26
- async createStores(data, sharedContext = {}) {
28
+ __joinerConfig() {
29
+ return joiner_config_1.joinerConfig;
30
+ }
31
+ async create(data, sharedContext = {}) {
27
32
  const input = Array.isArray(data) ? data : [data];
28
33
  const result = await this.create_(input, sharedContext);
29
34
  return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
@@ -31,9 +36,9 @@ class StoreModuleService extends (0, utils_1.MedusaService)({ Store: _models_1.S
31
36
  async create_(data, sharedContext = {}) {
32
37
  let normalizedInput = StoreModuleService.normalizeInput(data);
33
38
  StoreModuleService.validateCreateRequest(normalizedInput);
34
- return (await this.storeService_.upsertWithReplace(normalizedInput, { relations: ["supported_currencies"] }, sharedContext)).entities;
39
+ return await this.storeService_.create(normalizedInput, sharedContext);
35
40
  }
36
- async upsertStores(data, sharedContext = {}) {
41
+ async upsert(data, sharedContext = {}) {
37
42
  const input = Array.isArray(data) ? data : [data];
38
43
  const forUpdate = input.filter((store) => !!store.id);
39
44
  const forCreate = input.filter((store) => !store.id);
@@ -47,7 +52,7 @@ class StoreModuleService extends (0, utils_1.MedusaService)({ Store: _models_1.S
47
52
  const result = (await (0, utils_1.promiseAll)(operations)).flat();
48
53
  return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
49
54
  }
50
- async updateStores(idOrSelector, data, sharedContext = {}) {
55
+ async update(idOrSelector, data, sharedContext = {}) {
51
56
  let normalizedInput = [];
52
57
  if ((0, utils_1.isString)(idOrSelector)) {
53
58
  normalizedInput = [{ id: idOrSelector, ...data }];
@@ -65,43 +70,50 @@ class StoreModuleService extends (0, utils_1.MedusaService)({ Store: _models_1.S
65
70
  }
66
71
  async update_(data, sharedContext = {}) {
67
72
  const normalizedInput = StoreModuleService.normalizeInput(data);
68
- StoreModuleService.validateUpdateRequest(normalizedInput);
69
- return (await this.storeService_.upsertWithReplace(normalizedInput, { relations: ["supported_currencies"] }, sharedContext)).entities;
73
+ await this.validateUpdateRequest(normalizedInput);
74
+ return await this.storeService_.update(normalizedInput, sharedContext);
70
75
  }
71
76
  static normalizeInput(stores) {
72
77
  return stores.map((store) => (0, utils_1.removeUndefined)({
73
78
  ...store,
74
- supported_currencies: store.supported_currencies?.map((c) => ({
75
- ...c,
76
- currency_code: c.currency_code.toLowerCase(),
77
- })),
78
79
  name: store.name?.trim(),
79
80
  }));
80
81
  }
81
82
  static validateCreateRequest(stores) {
82
83
  for (const store of stores) {
83
- if (store.supported_currencies?.length) {
84
- const duplicates = (0, utils_1.getDuplicates)(store.supported_currencies?.map((c) => c.currency_code));
85
- if (duplicates.length) {
86
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Duplicate currency codes: ${duplicates.join(", ")}`);
87
- }
88
- let seenDefault = false;
89
- store.supported_currencies?.forEach((c) => {
90
- if (c.is_default) {
91
- if (seenDefault) {
92
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Only one default currency is allowed`);
93
- }
94
- seenDefault = true;
95
- }
96
- });
97
- if (!seenDefault) {
98
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `There should be a default currency set for the store`);
84
+ // If we are setting the default currency code on creating, make sure it is supported
85
+ if (store.default_currency_code) {
86
+ if (!store.supported_currency_codes?.includes(store.default_currency_code ?? "")) {
87
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Store does not have currency: ${store.default_currency_code}`);
99
88
  }
100
89
  }
101
90
  }
102
91
  }
103
- static validateUpdateRequest(stores) {
104
- StoreModuleService.validateCreateRequest(stores);
92
+ async validateUpdateRequest(stores) {
93
+ const dbStores = await this.storeService_.list({ id: stores.map((s) => s.id) }, { take: null });
94
+ const dbStoresMap = new Map(dbStores.map((dbStore) => [dbStore.id, dbStore]));
95
+ for (const store of stores) {
96
+ const dbStore = dbStoresMap.get(store.id);
97
+ // If it is updating both the supported currency codes and the default one, look in that list
98
+ if (store.supported_currency_codes && store.default_currency_code) {
99
+ if (!store.supported_currency_codes.includes(store.default_currency_code ?? "")) {
100
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Store does not have currency: ${store.default_currency_code}`);
101
+ }
102
+ return;
103
+ }
104
+ // If it is updating only the default currency code, look in the db store
105
+ if (store.default_currency_code) {
106
+ if (!dbStore?.supported_currency_codes?.includes(store.default_currency_code)) {
107
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Store does not have currency: ${store.default_currency_code}`);
108
+ }
109
+ }
110
+ // If it is updating only the supported currency codes, make sure one of them is not set as a default one
111
+ if (store.supported_currency_codes) {
112
+ if (!store.supported_currency_codes.includes(dbStore?.default_currency_code ?? "")) {
113
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, "You are not allowed to remove default currency from store currencies without replacing it as well");
114
+ }
115
+ }
116
+ }
105
117
  }
106
118
  }
107
119
  exports.default = StoreModuleService;
@@ -111,7 +123,7 @@ __decorate([
111
123
  __metadata("design:type", Function),
112
124
  __metadata("design:paramtypes", [Object, Object]),
113
125
  __metadata("design:returntype", Promise)
114
- ], StoreModuleService.prototype, "createStores", null);
126
+ ], StoreModuleService.prototype, "create", null);
115
127
  __decorate([
116
128
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
117
129
  __param(1, (0, utils_1.MedusaContext)()),
@@ -125,14 +137,14 @@ __decorate([
125
137
  __metadata("design:type", Function),
126
138
  __metadata("design:paramtypes", [Object, Object]),
127
139
  __metadata("design:returntype", Promise)
128
- ], StoreModuleService.prototype, "upsertStores", null);
140
+ ], StoreModuleService.prototype, "upsert", null);
129
141
  __decorate([
130
142
  (0, utils_1.InjectManager)("baseRepository_"),
131
143
  __param(2, (0, utils_1.MedusaContext)()),
132
144
  __metadata("design:type", Function),
133
145
  __metadata("design:paramtypes", [Object, Object, Object]),
134
146
  __metadata("design:returntype", Promise)
135
- ], StoreModuleService.prototype, "updateStores", null);
147
+ ], StoreModuleService.prototype, "update", null);
136
148
  __decorate([
137
149
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
138
150
  __param(1, (0, utils_1.MedusaContext)()),
@@ -2,9 +2,8 @@ import { StoreTypes } from "@medusajs/types";
2
2
  import { IEventBusModuleService, Logger } from "@medusajs/types";
3
3
  export type InitializeModuleInjectableDependencies = {
4
4
  logger?: Logger;
5
- eventBusModuleService?: IEventBusModuleService;
5
+ eventBusService?: IEventBusModuleService;
6
6
  };
7
7
  export type UpdateStoreInput = StoreTypes.UpdateStoreDTO & {
8
8
  id: string;
9
9
  };
10
- //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/store",
3
- "version": "0.1.2-snapshot-20240718073308",
3
+ "version": "0.1.2-snapshot-20240718083016",
4
4
  "description": "Medusa Store module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,10 @@
8
8
  "dist"
9
9
  ],
10
10
  "engines": {
11
- "node": ">=20"
11
+ "node": ">=16"
12
+ },
13
+ "bin": {
14
+ "medusa-store-seed": "dist/scripts/bin/run-seed.js"
12
15
  },
13
16
  "repository": {
14
17
  "type": "git",
@@ -34,22 +37,25 @@
34
37
  "orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
35
38
  },
36
39
  "devDependencies": {
37
- "@medusajs/modules-sdk": "1.13.0-snapshot-20240718073308",
38
- "@medusajs/types": "1.12.0-snapshot-20240718073308",
39
40
  "@mikro-orm/cli": "5.9.7",
40
41
  "cross-env": "^5.2.1",
41
- "jest": "^29.7.0",
42
+ "jest": "^29.6.3",
42
43
  "medusa-test-utils": "workspace:^",
43
44
  "rimraf": "^3.0.2",
45
+ "ts-jest": "^29.1.1",
44
46
  "ts-node": "^10.9.1",
45
47
  "tsc-alias": "^1.8.6",
46
48
  "typescript": "^5.1.6"
47
49
  },
48
50
  "dependencies": {
49
- "@medusajs/utils": "1.12.0-snapshot-20240718073308",
51
+ "@medusajs/modules-sdk": "1.12.12-snapshot-20240718083016",
52
+ "@medusajs/types": "1.11.17-snapshot-20240718083016",
53
+ "@medusajs/utils": "1.11.10-snapshot-20240718083016",
50
54
  "@mikro-orm/core": "5.9.7",
51
55
  "@mikro-orm/migrations": "5.9.7",
52
56
  "@mikro-orm/postgresql": "5.9.7",
53
- "awilix": "^8.0.0"
57
+ "awilix": "^8.0.0",
58
+ "dotenv": "^16.4.5",
59
+ "knex": "2.4.2"
54
60
  }
55
61
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;;;;AAG9C,wBAEE"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"InitialSetup20240227075933.d.ts","sourceRoot":"","sources":["../../src/migrations/InitialSetup20240227075933.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,qBAAa,0BAA2B,SAAQ,SAAS;IACjD,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC;CA8D1B"}
@@ -1,5 +0,0 @@
1
- import { Migration } from "@mikro-orm/migrations";
2
- export declare class Migration20240621145944 extends Migration {
3
- up(): Promise<void>;
4
- }
5
- //# sourceMappingURL=Migration20240621145944.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Migration20240621145944.d.ts","sourceRoot":"","sources":["../../src/migrations/Migration20240621145944.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,qBAAa,uBAAwB,SAAQ,SAAS;IAC9C,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC;CAiB1B"}
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Migration20240621145944 = void 0;
4
- const migrations_1 = require("@mikro-orm/migrations");
5
- class Migration20240621145944 extends migrations_1.Migration {
6
- async up() {
7
- this.addSql('create table if not exists "store_currency" ("id" text not null, "currency_code" text not null, "is_default" boolean not null default false, "store_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "store_currency_pkey" primary key ("id"));');
8
- this.addSql('CREATE INDEX IF NOT EXISTS "IDX_store_currency_deleted_at" ON "store_currency" (deleted_at) WHERE deleted_at IS NOT NULL;');
9
- this.addSql('alter table if exists "store_currency" add constraint "store_currency_store_id_foreign" foreign key ("store_id") references "store" ("id") on update cascade on delete cascade;');
10
- this.addSql('alter table if exists "store" drop column if exists "supported_currency_codes";');
11
- this.addSql('alter table if exists "store" drop column if exists "default_currency_code";');
12
- }
13
- }
14
- exports.Migration20240621145944 = Migration20240621145944;
@@ -1,14 +0,0 @@
1
- import Store from "./store";
2
- export default class StoreCurrency {
3
- id: string;
4
- currency_code: string;
5
- is_default?: boolean;
6
- store_id: string | null;
7
- store: Store | null;
8
- created_at: Date;
9
- updated_at: Date;
10
- deleted_at: Date | null;
11
- onCreate(): void;
12
- onInit(): void;
13
- }
14
- //# sourceMappingURL=currency.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../../src/models/currency.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,MAAM,SAAS,CAAA;AAU3B,MAAM,CAAC,OAAO,OAAO,aAAa;IAEhC,EAAE,EAAE,MAAM,CAAA;IAIV,aAAa,EAAE,MAAM,CAAA;IAGrB,UAAU,CAAC,EAAE,OAAO,CAAA;IASpB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IAMvB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;IAOnB,UAAU,EAAE,IAAI,CAAA;IAQhB,UAAU,EAAE,IAAI,CAAA;IAIhB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAO;IAG9B,QAAQ;IAKR,MAAM;CAGP"}
@@ -1,102 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const utils_1 = require("@medusajs/utils");
16
- const core_1 = require("@mikro-orm/core");
17
- const store_1 = __importDefault(require("./store"));
18
- const StoreCurrencyDeletedAtIndex = (0, utils_1.createPsqlIndexStatementHelper)({
19
- tableName: "store_currency",
20
- columns: "deleted_at",
21
- where: "deleted_at IS NOT NULL",
22
- });
23
- let StoreCurrency = class StoreCurrency {
24
- constructor() {
25
- this.deleted_at = null;
26
- }
27
- onCreate() {
28
- this.id = (0, utils_1.generateEntityId)(this.id, "stocur");
29
- }
30
- onInit() {
31
- this.id = (0, utils_1.generateEntityId)(this.id, "stocur");
32
- }
33
- };
34
- __decorate([
35
- (0, core_1.PrimaryKey)({ columnType: "text" }),
36
- __metadata("design:type", String)
37
- ], StoreCurrency.prototype, "id", void 0);
38
- __decorate([
39
- (0, utils_1.Searchable)(),
40
- (0, core_1.Property)({ columnType: "text" }),
41
- __metadata("design:type", String)
42
- ], StoreCurrency.prototype, "currency_code", void 0);
43
- __decorate([
44
- (0, core_1.Property)({ columnType: "boolean", default: false }),
45
- __metadata("design:type", Boolean)
46
- ], StoreCurrency.prototype, "is_default", void 0);
47
- __decorate([
48
- (0, core_1.ManyToOne)(() => store_1.default, {
49
- columnType: "text",
50
- fieldName: "store_id",
51
- mapToPk: true,
52
- nullable: true,
53
- onDelete: "cascade",
54
- }),
55
- __metadata("design:type", Object)
56
- ], StoreCurrency.prototype, "store_id", void 0);
57
- __decorate([
58
- (0, core_1.ManyToOne)(() => store_1.default, {
59
- persist: false,
60
- nullable: true,
61
- }),
62
- __metadata("design:type", Object)
63
- ], StoreCurrency.prototype, "store", void 0);
64
- __decorate([
65
- (0, core_1.Property)({
66
- onCreate: () => new Date(),
67
- columnType: "timestamptz",
68
- defaultRaw: "now()",
69
- }),
70
- __metadata("design:type", Date)
71
- ], StoreCurrency.prototype, "created_at", void 0);
72
- __decorate([
73
- (0, core_1.Property)({
74
- onCreate: () => new Date(),
75
- onUpdate: () => new Date(),
76
- columnType: "timestamptz",
77
- defaultRaw: "now()",
78
- }),
79
- __metadata("design:type", Date)
80
- ], StoreCurrency.prototype, "updated_at", void 0);
81
- __decorate([
82
- StoreCurrencyDeletedAtIndex.MikroORMIndex(),
83
- (0, core_1.Property)({ columnType: "timestamptz", nullable: true }),
84
- __metadata("design:type", Object)
85
- ], StoreCurrency.prototype, "deleted_at", void 0);
86
- __decorate([
87
- (0, core_1.BeforeCreate)(),
88
- __metadata("design:type", Function),
89
- __metadata("design:paramtypes", []),
90
- __metadata("design:returntype", void 0)
91
- ], StoreCurrency.prototype, "onCreate", null);
92
- __decorate([
93
- (0, core_1.OnInit)(),
94
- __metadata("design:type", Function),
95
- __metadata("design:paramtypes", []),
96
- __metadata("design:returntype", void 0)
97
- ], StoreCurrency.prototype, "onInit", null);
98
- StoreCurrency = __decorate([
99
- (0, core_1.Entity)(),
100
- (0, core_1.Filter)(utils_1.DALUtils.mikroOrmSoftDeletableFilterOptions)
101
- ], StoreCurrency);
102
- exports.default = StoreCurrency;
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/models/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,SAAS,CAAA;AAC1C,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,YAAY,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/models/store.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAErC,OAAO,EAOL,aAAa,EAEb,UAAU,EAEX,MAAM,iBAAiB,CAAA;AACxB,OAAO,aAAa,MAAM,YAAY,CAAA;AAEtC,KAAK,kBAAkB,GAAG,GAAG,CAAC,6BAA6B,CAAA;AAU3D,MAAM,CAAC,OAAO,OAAO,KAAK;IACxB,CAAC,aAAa,CAAC,CAAC,EAAE,kBAAkB,CAAA;IAGpC,EAAE,EAAE,MAAM,CAAA;IAIV,IAAI,EAAE,MAAM,CAAA;IAKZ,oBAAoB,oCAAsC;IAG1D,wBAAwB,EAAE,MAAM,GAAG,IAAI,CAAO;IAG9C,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAO;IAGvC,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAO;IAGzC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAO;IAO/C,UAAU,EAAE,IAAI,CAAA;IAQhB,UAAU,EAAE,IAAI,CAAA;IAIhB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAO;IAG9B,QAAQ;IAKR,MAAM;CAGP"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,wBAAwB,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"store-module-service.d.ts","sourceRoot":"","sources":["../../src/services/store-module-service.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,GAAG,EACH,yBAAyB,EACzB,mBAAmB,EACnB,eAAe,EACf,UAAU,EACX,MAAM,iBAAiB,CAAA;AAaxB,OAAO,EAAE,KAAK,EAAiB,MAAM,SAAS,CAAA;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAA;AAEzC,KAAK,oBAAoB,GAAG;IAC1B,cAAc,EAAE,GAAG,CAAC,iBAAiB,CAAA;IACrC,YAAY,EAAE,eAAe,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAA;CAC1D,CAAA;;WAIU;QAAE,GAAG,EAAE,WAAW,QAAQ,CAAA;KAAE;mBACpB;QAAE,GAAG,EAAE,WAAW,gBAAgB,CAAA;KAAE;;AAHvD,MAAM,CAAC,OAAO,OAAO,kBACnB,SAAQ,uBAIR,YAAW,mBAAmB;IAO5B,SAAS,CAAC,QAAQ,CAAC,iBAAiB,EAAE,yBAAyB;IALjE,SAAS,CAAC,eAAe,EAAE,GAAG,CAAC,iBAAiB,CAAA;IAChD,SAAS,CAAC,QAAQ,CAAC,aAAa,EAAE,eAAe,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAA;gBAG7E,EAAE,cAAc,EAAE,YAAY,EAAE,EAAE,oBAAoB,EACnC,iBAAiB,EAAE,yBAAyB;IAS3D,YAAY,CAChB,IAAI,EAAE,UAAU,CAAC,cAAc,EAAE,EACjC,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;IAC3B,YAAY,CAChB,IAAI,EAAE,UAAU,CAAC,cAAc,EAC/B,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC;IAgBzB,OAAO,CACX,IAAI,EAAE,UAAU,CAAC,cAAc,EAAE,EAChB,aAAa,GAAE,OAAY,GAC3C,OAAO,CAAC,KAAK,EAAE,CAAC;IAab,YAAY,CAChB,IAAI,EAAE,UAAU,CAAC,cAAc,EAAE,EACjC,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;IAC3B,YAAY,CAChB,IAAI,EAAE,UAAU,CAAC,cAAc,EAC/B,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC;IA8BzB,YAAY,CAChB,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,UAAU,CAAC,cAAc,EAC/B,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC;IACzB,YAAY,CAChB,QAAQ,EAAE,UAAU,CAAC,oBAAoB,EACzC,IAAI,EAAE,UAAU,CAAC,cAAc,EAC/B,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;cAiCjB,OAAO,CACrB,IAAI,EAAE,gBAAgB,EAAE,EACP,aAAa,GAAE,OAAY,GAC3C,OAAO,CAAC,KAAK,EAAE,CAAC;IAanB,OAAO,CAAC,MAAM,CAAC,cAAc;IAe7B,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAuCpC,OAAO,CAAC,MAAM,CAAC,qBAAqB;CAGrC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAEhE,MAAM,MAAM,sCAAsC,GAAG;IACnD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,qBAAqB,CAAC,EAAE,sBAAsB,CAAA;CAC/C,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,cAAc,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CAAA"}