diff --git a/packages/store/integration-tests/__fixtures__/index.ts b/packages/store/integration-tests/__fixtures__/index.ts index e69de29bb2d1d..39b33936be3c9 100644 --- a/packages/store/integration-tests/__fixtures__/index.ts +++ b/packages/store/integration-tests/__fixtures__/index.ts @@ -0,0 +1,10 @@ +import { StoreTypes } from "@medusajs/types" + +export const createStoreFixture: StoreTypes.CreateStoreDTO = { + name: "Test store", + default_sales_channel_id: "test-sales-channel", + default_region_id: "test-region", + metadata: { + test: "test", + }, +} diff --git a/packages/store/integration-tests/__tests__/store-module-service.spec.ts b/packages/store/integration-tests/__tests__/store-module-service.spec.ts index f680e7ae172a7..5c8ca1b894da4 100644 --- a/packages/store/integration-tests/__tests__/store-module-service.spec.ts +++ b/packages/store/integration-tests/__tests__/store-module-service.spec.ts @@ -1,6 +1,7 @@ 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) @@ -11,9 +12,88 @@ moduleIntegrationTestRunner({ service, }: SuiteOptions) => { describe("Store Module Service", () => { - describe("noop", function () { - it("should run", function () { - expect(true).toBe(true) + 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", + default_sales_channel_id: "test-sales-channel", + default_region_id: "test-region", + metadata: { + test: "test", + }, + }) + ) + }) + }) + + 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") + }) + }) + + 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"]) + ) }) }) }) diff --git a/packages/store/src/migrations/.snapshot-medusa-store.json b/packages/store/src/migrations/.snapshot-medusa-store.json new file mode 100644 index 0000000000000..cf797f7239362 --- /dev/null +++ b/packages/store/src/migrations/.snapshot-medusa-store.json @@ -0,0 +1,92 @@ +{ + "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, + "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" + } + }, + "name": "store", + "schema": "public", + "indexes": [ + { + "keyName": "store_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + } + ] +} diff --git a/packages/store/src/migrations/InitialSetup20240226130829.ts b/packages/store/src/migrations/InitialSetup20240226130829.ts new file mode 100644 index 0000000000000..121cf6ad8602e --- /dev/null +++ b/packages/store/src/migrations/InitialSetup20240226130829.ts @@ -0,0 +1,9 @@ +import { Migration } from "@mikro-orm/migrations" + +export class InitialSetup20240226130829 extends Migration { + async up(): Promise { + this.addSql( + 'create table if not exists "store" ("id" text not null, "name" text not 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(), constraint "store_pkey" primary key ("id"));' + ) + } +} diff --git a/packages/store/src/models/store.ts b/packages/store/src/models/store.ts index 4b5d073382403..04e8f7d6b42cf 100644 --- a/packages/store/src/models/store.ts +++ b/packages/store/src/models/store.ts @@ -13,6 +13,21 @@ export default class Store { @PrimaryKey({ columnType: "text" }) id: string + @Property({ columnType: "text" }) + name: string + + @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 | null = null + @Property({ onCreate: () => new Date(), columnType: "timestamptz", diff --git a/packages/store/src/services/store-module-service.ts b/packages/store/src/services/store-module-service.ts index 7f861cd1e8837..adc269aaa3354 100644 --- a/packages/store/src/services/store-module-service.ts +++ b/packages/store/src/services/store-module-service.ts @@ -5,11 +5,21 @@ import { ModulesSdkTypes, IStoreModuleService, StoreTypes, + Context, } from "@medusajs/types" -import { ModulesSdkUtils } from "@medusajs/utils" +import { + InjectManager, + InjectTransactionManager, + MedusaContext, + ModulesSdkUtils, + isString, + promiseAll, + removeUndefined, +} from "@medusajs/utils" import { Store } from "@models" import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config" +import { UpdateStoreInput } from "@types" const generateMethodForModels = [] @@ -44,4 +54,132 @@ export default class StoreModuleService __joinerConfig(): ModuleJoinerConfig { return joinerConfig } + + async create( + data: StoreTypes.CreateStoreDTO[], + sharedContext?: Context + ): Promise + async create( + data: StoreTypes.CreateStoreDTO, + sharedContext?: Context + ): Promise + @InjectManager("baseRepository_") + async create( + data: StoreTypes.CreateStoreDTO | StoreTypes.CreateStoreDTO[], + @MedusaContext() sharedContext: Context = {} + ): Promise { + const input = Array.isArray(data) ? data : [data] + + const result = await this.create_(input, sharedContext) + + return await this.baseRepository_.serialize( + Array.isArray(data) ? result : result[0] + ) + } + + @InjectTransactionManager("baseRepository_") + async create_( + data: StoreTypes.CreateStoreDTO[], + @MedusaContext() sharedContext: Context = {} + ): Promise { + let normalizedInput = StoreModuleService.normalizeInput(data) + return await this.storeService_.create(normalizedInput, sharedContext) + } + + async upsert( + data: StoreTypes.UpsertStoreDTO[], + sharedContext?: Context + ): Promise + async upsert( + data: StoreTypes.UpsertStoreDTO, + sharedContext?: Context + ): Promise + @InjectTransactionManager("baseRepository_") + async upsert( + data: StoreTypes.UpsertStoreDTO | StoreTypes.UpsertStoreDTO[], + @MedusaContext() sharedContext: Context = {} + ): Promise { + 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[] = [] + + 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 + async update( + selector: StoreTypes.FilterableStoreProps, + data: StoreTypes.UpdateStoreDTO, + sharedContext?: Context + ): Promise + @InjectManager("baseRepository_") + async update( + idOrSelector: string | StoreTypes.FilterableStoreProps, + data: StoreTypes.UpdateStoreDTO, + @MedusaContext() sharedContext: Context = {} + ): Promise { + 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 { + const normalizedInput = StoreModuleService.normalizeInput(data) + return await this.storeService_.update(normalizedInput, sharedContext) + } + + private static normalizeInput( + stores: T[] + ): T[] { + return stores.map((store) => + removeUndefined({ + ...store, + name: store.name?.trim(), + }) + ) + } } diff --git a/packages/store/src/types/index.ts b/packages/store/src/types/index.ts index fdac085753676..93ec9a4524553 100644 --- a/packages/store/src/types/index.ts +++ b/packages/store/src/types/index.ts @@ -1,6 +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 } diff --git a/packages/types/src/store/common/store.ts b/packages/types/src/store/common/store.ts index 08792fb31f0fc..d013612e5bb42 100644 --- a/packages/types/src/store/common/store.ts +++ b/packages/types/src/store/common/store.ts @@ -1,3 +1,15 @@ +import { BaseFilterable } from "../../dal" + export interface StoreDTO { id: string + name: string + default_sales_channel_id?: string + default_region_id?: string + default_location_id?: string + metadata: Record | null +} +export interface FilterableStoreProps + extends BaseFilterable { + id?: string | string[] + name?: string | string[] } diff --git a/packages/types/src/store/mutations/store.ts b/packages/types/src/store/mutations/store.ts index 0b0e8b2d01c65..dfb47ba985538 100644 --- a/packages/types/src/store/mutations/store.ts +++ b/packages/types/src/store/mutations/store.ts @@ -1 +1,24 @@ -export interface CreateStoreDTO {} +export interface CreateStoreDTO { + name: string + default_sales_channel_id?: string + default_region_id?: string + default_location_id?: string + metadata?: Record +} + +export interface UpsertStoreDTO { + id?: string + name?: string + default_sales_channel_id?: string + default_region_id?: string + default_location_id?: string + metadata?: Record +} + +export interface UpdateStoreDTO { + name?: string + default_sales_channel_id?: string + default_region_id?: string + default_location_id?: string + metadata?: Record +} diff --git a/packages/types/src/store/service.ts b/packages/types/src/store/service.ts index 595c7c74c8a1f..c2c2ebc973c41 100644 --- a/packages/types/src/store/service.ts +++ b/packages/types/src/store/service.ts @@ -1,3 +1,213 @@ +import { FindConfig } from "../common" import { IModuleService } from "../modules-sdk" +import { Context } from "../shared-context" +import { FilterableStoreProps, StoreDTO } from "./common" +import { CreateStoreDTO, UpdateStoreDTO, UpsertStoreDTO } from "./mutations" -export interface IStoreModuleService extends IModuleService {} +/** + * The main service interface for the store module. + */ +export interface IStoreModuleService extends IModuleService { + /** + * This method creates stores. + * + * @param {CreateStoreDTO[]} data - The stores to be created. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The created stores. + * + * @example + * {example-code} + */ + create(data: CreateStoreDTO[], sharedContext?: Context): Promise + + /** + * This method creates a store. + * + * @param {CreateStoreDTO} data - The store to be created. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The created store. + * + * @example + * {example-code} + */ + create(data: CreateStoreDTO, sharedContext?: Context): Promise + + /** + * This method updates existing stores, or creates new ones if they don't exist. + * + * @param {UpsertStoreDTO[]} data - The attributes to update or create in each store. + * @returns {Promise} The updated and created stores. + * + * @example + * {example-code} + */ + upsert(data: UpsertStoreDTO[], sharedContext?: Context): Promise + + /** + * This method updates an existing store, or creates a new one if it doesn't exist. + * + * @param {UpsertStoreDTO} data - The attributes to update or create for the store. + * @returns {Promise} The updated or created store. + * + * @example + * {example-code} + */ + upsert(data: UpsertStoreDTO, sharedContext?: Context): Promise + + /** + * This method updates an existing store. + * + * @param {string} id - The store's ID. + * @param {UpdateStoreDTO} data - The details to update in the store. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The updated store. + */ + update( + id: string, + data: UpdateStoreDTO, + sharedContext?: Context + ): Promise + + /** + * This method updates existing stores. + * + * @param {FilterableStoreProps} selector - The filters to specify which stores should be updated. + * @param {UpdateStoreDTO} data - The details to update in the stores. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The updated stores. + * + * @example + * {example-code} + */ + update( + selector: FilterableStoreProps, + data: UpdateStoreDTO, + sharedContext?: Context + ): Promise + + /** + * This method deletes stores by their IDs. + * + * @param {string[]} ids - The list of IDs of stores to delete. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} Resolves when the stores are deleted. + * + * @example + * {example-code} + */ + delete(ids: string[], sharedContext?: Context): Promise + + /** + * This method deletes a store by its ID. + * + * @param {string} id - The ID of the store. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} Resolves when the store is deleted. + * + * @example + * {example-code} + */ + delete(id: string, sharedContext?: Context): Promise + + /** + * This method retrieves a store by its ID. + * + * @param {string} id - The ID of the retrieve. + * @param {FindConfig} config - The configurations determining how the store is retrieved. Its properties, such as `select` or `relations`, accept the + * attributes or relations associated with a store. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The retrieved store. + * + * @example + * A simple example that retrieves a {type name} by its ID: + * + * ```ts + * {example-code} + * ``` + * + * To specify relations that should be retrieved: + * + * ```ts + * {example-code} + * ``` + * + * + */ + retrieve( + id: string, + config?: FindConfig, + sharedContext?: Context + ): Promise + + /** + * This method retrieves a paginated list of stores based on optional filters and configuration. + * + * @param {FilterableStoreProps} filters - The filters to apply on the retrieved store. + * @param {FindConfig} config - The configurations determining how the store is retrieved. Its properties, such as `select` or `relations`, accept the + * attributes or relations associated with a store. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The list of stores. + * + * @example + * To retrieve a list of {type name} using their IDs: + * + * ```ts + * {example-code} + * ``` + * + * To specify relations that should be retrieved within the {type name}: + * + * ```ts + * {example-code} + * ``` + * + * By default, only the first `{default limit}` records are retrieved. You can control pagination by specifying the `skip` and `take` properties of the `config` parameter: + * + * ```ts + * {example-code} + * ``` + * + * + */ + list( + filters?: FilterableStoreProps, + config?: FindConfig, + sharedContext?: Context + ): Promise + + /** + * This method retrieves a paginated list of stores along with the total count of available stores satisfying the provided filters. + * + * @param {FilterableStoreProps} filters - The filters to apply on the retrieved store. + * @param {FindConfig} config - The configurations determining how the store is retrieved. Its properties, such as `select` or `relations`, accept the + * attributes or relations associated with a store. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise<[StoreDTO[], number]>} The list of stores along with their total count. + * + * @example + * To retrieve a list of {type name} using their IDs: + * + * ```ts + * {example-code} + * ``` + * + * To specify relations that should be retrieved within the {type name}: + * + * ```ts + * {example-code} + * ``` + * + * By default, only the first `{default limit}` records are retrieved. You can control pagination by specifying the `skip` and `take` properties of the `config` parameter: + * + * ```ts + * {example-code} + * ``` + * + * + */ + listAndCount( + filters?: FilterableStoreProps, + config?: FindConfig, + sharedContext?: Context + ): Promise<[StoreDTO[], number]> +}