@medusajs/store 0.1.2-snapshot-20240607093839 → 0.1.2-snapshot-20240701122250

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { moduleDefinition } from "./module-definition";
2
- export * from "./types";
3
- export * from "./models";
4
- export * from "./services";
1
+ import { ModuleExports } from "@medusajs/types";
2
+ export declare const moduleDefinition: ModuleExports;
5
3
  export default moduleDefinition;
package/dist/index.js CHANGED
@@ -1,21 +1,8 @@
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
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- const module_definition_1 = require("./module-definition");
18
- __exportStar(require("./types"), exports);
19
- __exportStar(require("./models"), exports);
20
- __exportStar(require("./services"), exports);
21
- exports.default = module_definition_1.moduleDefinition;
3
+ exports.moduleDefinition = void 0;
4
+ const _services_1 = require("./services");
5
+ exports.moduleDefinition = {
6
+ service: _services_1.StoreModuleService,
7
+ };
8
+ exports.default = exports.moduleDefinition;
@@ -1,5 +1,3 @@
1
- import { ModuleJoinerConfig } from "@medusajs/types";
2
1
  import { MapToConfig } from "@medusajs/utils";
3
- export declare const LinkableKeys: Record<string, string>;
2
+ export declare const joinerConfig: Omit<import("@medusajs/types").ModuleJoinerConfig, "serviceName" | "primaryKeys" | "alias" | "linkableKeys"> & Required<Pick<import("@medusajs/types").ModuleJoinerConfig, "serviceName" | "primaryKeys" | "alias" | "linkableKeys">>;
4
3
  export declare const entityNameToLinkableKeysMap: MapToConfig;
5
- export declare const joinerConfig: ModuleJoinerConfig;
@@ -1,29 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  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] ??= [];
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
- };
3
+ exports.entityNameToLinkableKeysMap = exports.joinerConfig = void 0;
4
+ const utils_1 = require("@medusajs/utils");
5
+ exports.joinerConfig = (0, utils_1.defineJoinerConfig)(utils_1.Modules.STORE);
6
+ exports.entityNameToLinkableKeysMap = (0, utils_1.buildEntitiesNameToLinkableKeysMap)(exports.joinerConfig.linkableKeys);
@@ -0,0 +1,4 @@
1
+ import { Migration } from "@mikro-orm/migrations";
2
+ export declare class Migration20240621145944 extends Migration {
3
+ up(): Promise<void>;
4
+ }
@@ -0,0 +1,14 @@
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;
@@ -0,0 +1,13 @@
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
+ }
@@ -0,0 +1,102 @@
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 +1,2 @@
1
1
  export { default as Store } from "./store";
2
+ export { default as StoreCurrency } from "./currency";
@@ -3,6 +3,8 @@ 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.Store = void 0;
6
+ exports.StoreCurrency = 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 } from "@mikro-orm/core";
2
+ import { OptionalProps, Collection } from "@mikro-orm/core";
3
+ import StoreCurrency from "./currency";
3
4
  type StoreOptionalProps = DAL.SoftDeletableEntityDateColumns;
4
5
  export default class Store {
5
6
  [OptionalProps]?: StoreOptionalProps;
6
7
  id: string;
7
8
  name: string;
8
- supported_currency_codes: string[];
9
- default_currency_code: string | null;
9
+ supported_currencies: Collection<StoreCurrency, object>;
10
10
  default_sales_channel_id: string | null;
11
11
  default_region_id: string | null;
12
12
  default_location_id: string | null;
@@ -8,9 +8,13 @@ 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
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  const utils_1 = require("@medusajs/utils");
13
16
  const core_1 = require("@mikro-orm/core");
17
+ const currency_1 = __importDefault(require("./currency"));
14
18
  const StoreDeletedAtIndex = (0, utils_1.createPsqlIndexStatementHelper)({
15
19
  tableName: "store",
16
20
  columns: "deleted_at",
@@ -18,8 +22,7 @@ const StoreDeletedAtIndex = (0, utils_1.createPsqlIndexStatementHelper)({
18
22
  });
19
23
  let Store = class Store {
20
24
  constructor() {
21
- this.supported_currency_codes = [];
22
- this.default_currency_code = null;
25
+ this.supported_currencies = new core_1.Collection(this);
23
26
  this.default_sales_channel_id = null;
24
27
  this.default_region_id = null;
25
28
  this.default_location_id = null;
@@ -43,13 +46,11 @@ __decorate([
43
46
  __metadata("design:type", String)
44
47
  ], Store.prototype, "name", void 0);
45
48
  __decorate([
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 }),
49
+ (0, core_1.OneToMany)(() => currency_1.default, (o) => o.store, {
50
+ cascade: [core_1.Cascade.PERSIST, "soft-remove"],
51
+ }),
51
52
  __metadata("design:type", Object)
52
- ], Store.prototype, "default_currency_code", void 0);
53
+ ], Store.prototype, "supported_currencies", void 0);
53
54
  __decorate([
54
55
  (0, core_1.Property)({ columnType: "text", nullable: true }),
55
56
  __metadata("design:type", Object)
@@ -1,32 +1,34 @@
1
- import { DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, IStoreModuleService, StoreTypes, Context } from "@medusajs/types";
2
- import { ModulesSdkUtils } from "@medusajs/utils";
1
+ import { Context, DAL, InternalModuleDeclaration, IStoreModuleService, ModuleJoinerConfig, ModulesSdkTypes, StoreTypes } from "@medusajs/types";
3
2
  import { Store } from "../models";
4
3
  import { UpdateStoreInput } from "../types";
5
4
  type InjectedDependencies = {
6
5
  baseRepository: DAL.RepositoryService;
7
- storeService: ModulesSdkTypes.InternalModuleService<any>;
6
+ storeService: ModulesSdkTypes.IMedusaInternalService<any>;
8
7
  };
9
- declare const StoreModuleService_base: new (container: InjectedDependencies) => ModulesSdkUtils.AbstractModuleService<InjectedDependencies, StoreTypes.StoreDTO, {
8
+ declare const StoreModuleService_base: new (...args: any[]) => import("@medusajs/utils").AbstractModuleService<{
10
9
  Store: {
11
10
  dto: StoreTypes.StoreDTO;
12
11
  };
12
+ StoreCurrency: {
13
+ dto: StoreTypes.StoreCurrencyDTO;
14
+ };
13
15
  }>;
14
- export default class StoreModuleService<TEntity extends Store = Store> extends StoreModuleService_base implements IStoreModuleService {
16
+ export default class StoreModuleService extends StoreModuleService_base implements IStoreModuleService {
15
17
  protected readonly moduleDeclaration: InternalModuleDeclaration;
16
18
  protected baseRepository_: DAL.RepositoryService;
17
- protected readonly storeService_: ModulesSdkTypes.InternalModuleService<TEntity>;
19
+ protected readonly storeService_: ModulesSdkTypes.IMedusaInternalService<Store>;
18
20
  constructor({ baseRepository, storeService }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
19
21
  __joinerConfig(): ModuleJoinerConfig;
20
- create(data: StoreTypes.CreateStoreDTO[], sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
21
- create(data: StoreTypes.CreateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
22
+ createStores(data: StoreTypes.CreateStoreDTO[], sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
23
+ createStores(data: StoreTypes.CreateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
22
24
  create_(data: StoreTypes.CreateStoreDTO[], sharedContext?: Context): Promise<Store[]>;
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[]>;
25
+ upsertStores(data: StoreTypes.UpsertStoreDTO[], sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
26
+ upsertStores(data: StoreTypes.UpsertStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
27
+ updateStores(id: string, data: StoreTypes.UpdateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO>;
28
+ updateStores(selector: StoreTypes.FilterableStoreProps, data: StoreTypes.UpdateStoreDTO, sharedContext?: Context): Promise<StoreTypes.StoreDTO[]>;
27
29
  protected update_(data: UpdateStoreInput[], sharedContext?: Context): Promise<Store[]>;
28
30
  private static normalizeInput;
29
31
  private static validateCreateRequest;
30
- private validateUpdateRequest;
32
+ private static validateUpdateRequest;
31
33
  }
32
34
  export {};
@@ -16,8 +16,7 @@ const types_1 = require("@medusajs/types");
16
16
  const utils_1 = require("@medusajs/utils");
17
17
  const _models_1 = require("../models");
18
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
+ class StoreModuleService extends (0, utils_1.MedusaService)({ Store: _models_1.Store, StoreCurrency: _models_1.StoreCurrency }, joiner_config_1.entityNameToLinkableKeysMap) {
21
20
  constructor({ baseRepository, storeService }, moduleDeclaration) {
22
21
  // @ts-ignore
23
22
  super(...arguments);
@@ -28,7 +27,7 @@ class StoreModuleService extends utils_1.ModulesSdkUtils.abstractModuleServiceFa
28
27
  __joinerConfig() {
29
28
  return joiner_config_1.joinerConfig;
30
29
  }
31
- async create(data, sharedContext = {}) {
30
+ async createStores(data, sharedContext = {}) {
32
31
  const input = Array.isArray(data) ? data : [data];
33
32
  const result = await this.create_(input, sharedContext);
34
33
  return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
@@ -36,9 +35,9 @@ class StoreModuleService extends utils_1.ModulesSdkUtils.abstractModuleServiceFa
36
35
  async create_(data, sharedContext = {}) {
37
36
  let normalizedInput = StoreModuleService.normalizeInput(data);
38
37
  StoreModuleService.validateCreateRequest(normalizedInput);
39
- return await this.storeService_.create(normalizedInput, sharedContext);
38
+ return (await this.storeService_.upsertWithReplace(normalizedInput, { relations: ["supported_currencies"] }, sharedContext)).entities;
40
39
  }
41
- async upsert(data, sharedContext = {}) {
40
+ async upsertStores(data, sharedContext = {}) {
42
41
  const input = Array.isArray(data) ? data : [data];
43
42
  const forUpdate = input.filter((store) => !!store.id);
44
43
  const forCreate = input.filter((store) => !store.id);
@@ -52,7 +51,7 @@ class StoreModuleService extends utils_1.ModulesSdkUtils.abstractModuleServiceFa
52
51
  const result = (await (0, utils_1.promiseAll)(operations)).flat();
53
52
  return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
54
53
  }
55
- async update(idOrSelector, data, sharedContext = {}) {
54
+ async updateStores(idOrSelector, data, sharedContext = {}) {
56
55
  let normalizedInput = [];
57
56
  if ((0, utils_1.isString)(idOrSelector)) {
58
57
  normalizedInput = [{ id: idOrSelector, ...data }];
@@ -70,51 +69,44 @@ class StoreModuleService extends utils_1.ModulesSdkUtils.abstractModuleServiceFa
70
69
  }
71
70
  async update_(data, sharedContext = {}) {
72
71
  const normalizedInput = StoreModuleService.normalizeInput(data);
73
- await this.validateUpdateRequest(normalizedInput);
74
- return await this.storeService_.update(normalizedInput, sharedContext);
72
+ StoreModuleService.validateUpdateRequest(normalizedInput);
73
+ return (await this.storeService_.upsertWithReplace(normalizedInput, { relations: ["supported_currencies"] }, sharedContext)).entities;
75
74
  }
76
75
  static normalizeInput(stores) {
77
76
  return stores.map((store) => (0, utils_1.removeUndefined)({
78
77
  ...store,
78
+ supported_currencies: store.supported_currencies?.map((c) => ({
79
+ ...c,
80
+ currency_code: c.currency_code.toLowerCase(),
81
+ })),
79
82
  name: store.name?.trim(),
80
83
  }));
81
84
  }
82
85
  static validateCreateRequest(stores) {
83
86
  for (const store of stores) {
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}`);
87
+ if (store.supported_currencies?.length) {
88
+ const duplicates = (0, utils_1.getDuplicates)(store.supported_currencies?.map((c) => c.currency_code));
89
+ if (duplicates.length) {
90
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Duplicate currency codes: ${duplicates.join(", ")}`);
88
91
  }
89
- }
90
- }
91
- }
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");
92
+ let seenDefault = false;
93
+ store.supported_currencies?.forEach((c) => {
94
+ if (c.is_default) {
95
+ if (seenDefault) {
96
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Only one default currency is allowed`);
97
+ }
98
+ seenDefault = true;
99
+ }
100
+ });
101
+ if (!seenDefault) {
102
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `There should be a default currency set for the store`);
114
103
  }
115
104
  }
116
105
  }
117
106
  }
107
+ static validateUpdateRequest(stores) {
108
+ StoreModuleService.validateCreateRequest(stores);
109
+ }
118
110
  }
119
111
  exports.default = StoreModuleService;
120
112
  __decorate([
@@ -123,7 +115,7 @@ __decorate([
123
115
  __metadata("design:type", Function),
124
116
  __metadata("design:paramtypes", [Object, Object]),
125
117
  __metadata("design:returntype", Promise)
126
- ], StoreModuleService.prototype, "create", null);
118
+ ], StoreModuleService.prototype, "createStores", null);
127
119
  __decorate([
128
120
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
129
121
  __param(1, (0, utils_1.MedusaContext)()),
@@ -137,14 +129,14 @@ __decorate([
137
129
  __metadata("design:type", Function),
138
130
  __metadata("design:paramtypes", [Object, Object]),
139
131
  __metadata("design:returntype", Promise)
140
- ], StoreModuleService.prototype, "upsert", null);
132
+ ], StoreModuleService.prototype, "upsertStores", null);
141
133
  __decorate([
142
134
  (0, utils_1.InjectManager)("baseRepository_"),
143
135
  __param(2, (0, utils_1.MedusaContext)()),
144
136
  __metadata("design:type", Function),
145
137
  __metadata("design:paramtypes", [Object, Object, Object]),
146
138
  __metadata("design:returntype", Promise)
147
- ], StoreModuleService.prototype, "update", null);
139
+ ], StoreModuleService.prototype, "updateStores", null);
148
140
  __decorate([
149
141
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
150
142
  __param(1, (0, utils_1.MedusaContext)()),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/store",
3
- "version": "0.1.2-snapshot-20240607093839",
3
+ "version": "0.1.2-snapshot-20240701122250",
4
4
  "description": "Medusa Store module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,10 +8,7 @@
8
8
  "dist"
9
9
  ],
10
10
  "engines": {
11
- "node": ">=16"
12
- },
13
- "bin": {
14
- "medusa-store-seed": "dist/scripts/bin/run-seed.js"
11
+ "node": ">=20"
15
12
  },
16
13
  "repository": {
17
14
  "type": "git",
@@ -37,20 +34,19 @@
37
34
  "orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
38
35
  },
39
36
  "devDependencies": {
40
- "@medusajs/types": "1.12.0-snapshot-20240607093839",
37
+ "@medusajs/types": "1.12.0-snapshot-20240701122250",
41
38
  "@mikro-orm/cli": "5.9.7",
42
39
  "cross-env": "^5.2.1",
43
- "jest": "^29.6.3",
40
+ "jest": "^29.7.0",
44
41
  "medusa-test-utils": "workspace:^",
45
42
  "rimraf": "^3.0.2",
46
- "ts-jest": "^29.1.1",
47
43
  "ts-node": "^10.9.1",
48
44
  "tsc-alias": "^1.8.6",
49
45
  "typescript": "^5.1.6"
50
46
  },
51
47
  "dependencies": {
52
- "@medusajs/modules-sdk": "1.13.0-snapshot-20240607093839",
53
- "@medusajs/utils": "1.12.0-snapshot-20240607093839",
48
+ "@medusajs/modules-sdk": "1.13.0-snapshot-20240701122250",
49
+ "@medusajs/utils": "1.12.0-snapshot-20240701122250",
54
50
  "@mikro-orm/core": "5.9.7",
55
51
  "@mikro-orm/migrations": "5.9.7",
56
52
  "@mikro-orm/postgresql": "5.9.7",
@@ -1,2 +0,0 @@
1
- import { ModuleExports } from "@medusajs/types";
2
- export declare const moduleDefinition: ModuleExports;
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.moduleDefinition = void 0;
4
- const _services_1 = require("./services");
5
- const service = _services_1.StoreModuleService;
6
- exports.moduleDefinition = {
7
- service,
8
- };
@@ -1 +0,0 @@
1
- export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils";
@@ -1,5 +0,0 @@
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; } });
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- declare const _default: Promise<void>;
3
- export default _default;
@@ -1,48 +0,0 @@
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
- })();