feat: Add basic endpoints and workflows for Store module (#6515)
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import { DataSource } from "typeorm"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("Store - Admin", () => {
|
||||
let dbConnection: DataSource
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let service: IStoreModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
service = appContainer.resolve(ModuleRegistrationName.STORE)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
|
||||
const existingStores = await service.list({})
|
||||
await service.delete(existingStores.map((s) => s.id))
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should correctly implement the entire lifecycle of a store", async () => {
|
||||
const api = useApi() as any
|
||||
const createdStore = await service.create({ name: "Test store" })
|
||||
|
||||
expect(createdStore).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdStore.id,
|
||||
name: "Test store",
|
||||
})
|
||||
)
|
||||
|
||||
const updated = await api.post(
|
||||
`/admin/stores/${createdStore.id}`,
|
||||
{
|
||||
name: "Updated store",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(updated.status).toEqual(200)
|
||||
expect(updated.data.store).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdStore.id,
|
||||
name: "Updated store",
|
||||
})
|
||||
)
|
||||
|
||||
await service.delete(createdStore.id)
|
||||
const listedStores = await api.get(`/admin/stores`, adminHeaders)
|
||||
expect(listedStores.data.stores).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
@@ -118,5 +118,10 @@ module.exports = {
|
||||
resources: "shared",
|
||||
resolve: "@medusajs/api-key",
|
||||
},
|
||||
[Modules.STORE]: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
resolve: "@medusajs/store",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"@medusajs/product": "workspace:^",
|
||||
"@medusajs/promotion": "workspace:^",
|
||||
"@medusajs/region": "workspace:^",
|
||||
"@medusajs/store": "workspace:^",
|
||||
"@medusajs/user": "workspace:^",
|
||||
"@medusajs/utils": "workspace:^",
|
||||
"@medusajs/workflow-engine-inmemory": "workspace:*",
|
||||
|
||||
@@ -8,3 +8,4 @@ export * from "./promotion"
|
||||
export * from "./region"
|
||||
export * from "./user"
|
||||
export * from "./api-key"
|
||||
export * from "./store"
|
||||
|
||||
2
packages/core-flows/src/store/index.ts
Normal file
2
packages/core-flows/src/store/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./steps"
|
||||
export * from "./workflows"
|
||||
34
packages/core-flows/src/store/steps/create-stores.ts
Normal file
34
packages/core-flows/src/store/steps/create-stores.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CreateStoreDTO, IStoreModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CreateStoresStepInput = {
|
||||
stores: CreateStoreDTO[]
|
||||
}
|
||||
|
||||
export const createStoresStepId = "create-stores"
|
||||
export const createStoresStep = createStep(
|
||||
createStoresStepId,
|
||||
async (data: CreateStoresStepInput, { container }) => {
|
||||
const service = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const created = await service.create(data.stores)
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((store) => store.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
await service.delete(createdIds)
|
||||
}
|
||||
)
|
||||
27
packages/core-flows/src/store/steps/delete-stores.ts
Normal file
27
packages/core-flows/src/store/steps/delete-stores.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IStoreModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const deleteStoresStepId = "delete-stores"
|
||||
export const deleteStoresStep = createStep(
|
||||
deleteStoresStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const storeModule = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
await storeModule.softDelete(ids)
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (idsToRestore, { container }) => {
|
||||
if (!idsToRestore?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const storeModule = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
await storeModule.restore(idsToRestore)
|
||||
}
|
||||
)
|
||||
3
packages/core-flows/src/store/steps/index.ts
Normal file
3
packages/core-flows/src/store/steps/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./create-stores"
|
||||
export * from "./delete-stores"
|
||||
export * from "./update-stores"
|
||||
51
packages/core-flows/src/store/steps/update-stores.ts
Normal file
51
packages/core-flows/src/store/steps/update-stores.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
FilterableStoreProps,
|
||||
IStoreModuleService,
|
||||
UpdateStoreDTO,
|
||||
} from "@medusajs/types"
|
||||
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type UpdateStoresStepInput = {
|
||||
selector: FilterableStoreProps
|
||||
update: UpdateStoreDTO
|
||||
}
|
||||
|
||||
export const updateStoresStepId = "update-stores"
|
||||
export const updateStoresStep = createStep(
|
||||
updateStoresStepId,
|
||||
async (data: UpdateStoresStepInput, { container }) => {
|
||||
const service = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
|
||||
data.update,
|
||||
])
|
||||
|
||||
const prevData = await service.list(data.selector, {
|
||||
select: selects,
|
||||
relations,
|
||||
})
|
||||
|
||||
const stores = await service.update(data.selector, data.update)
|
||||
return new StepResponse(stores, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
await service.upsert(
|
||||
prevData.map((r) => ({
|
||||
...r,
|
||||
metadata: r.metadata || undefined,
|
||||
}))
|
||||
)
|
||||
}
|
||||
)
|
||||
13
packages/core-flows/src/store/workflows/create-stores.ts
Normal file
13
packages/core-flows/src/store/workflows/create-stores.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { StoreDTO, CreateStoreDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createStoresStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { stores: CreateStoreDTO[] }
|
||||
|
||||
export const createStoresWorkflowId = "create-stores"
|
||||
export const createStoresWorkflow = createWorkflow(
|
||||
createStoresWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<StoreDTO[]> => {
|
||||
return createStoresStep(input)
|
||||
}
|
||||
)
|
||||
12
packages/core-flows/src/store/workflows/delete-stores.ts
Normal file
12
packages/core-flows/src/store/workflows/delete-stores.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteStoresStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteStoresWorkflowId = "delete-stores"
|
||||
export const deleteStoresWorkflow = createWorkflow(
|
||||
deleteStoresWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteStoresStep(input.ids)
|
||||
}
|
||||
)
|
||||
3
packages/core-flows/src/store/workflows/index.ts
Normal file
3
packages/core-flows/src/store/workflows/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./create-stores"
|
||||
export * from "./delete-stores"
|
||||
export * from "./update-stores"
|
||||
18
packages/core-flows/src/store/workflows/update-stores.ts
Normal file
18
packages/core-flows/src/store/workflows/update-stores.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { StoreDTO, FilterableStoreProps, UpdateStoreDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateStoresStep } from "../steps"
|
||||
|
||||
type UpdateStoresStepInput = {
|
||||
selector: FilterableStoreProps
|
||||
update: UpdateStoreDTO
|
||||
}
|
||||
|
||||
type WorkflowInput = UpdateStoresStepInput
|
||||
|
||||
export const updateStoresWorkflowId = "update-stores"
|
||||
export const updateStoresWorkflow = createWorkflow(
|
||||
updateStoresWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<StoreDTO[]> => {
|
||||
return updateStoresStep(input)
|
||||
}
|
||||
)
|
||||
36
packages/medusa/src/api-v2/admin/stores/[id]/route.ts
Normal file
36
packages/medusa/src/api-v2/admin/stores/[id]/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { updateStoresWorkflow } from "@medusajs/core-flows"
|
||||
import { UpdateStoreDTO } from "@medusajs/types"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { defaultAdminStoreFields } from "../query-config"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { id: req.params.id }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "store",
|
||||
variables,
|
||||
fields: defaultAdminStoreFields,
|
||||
})
|
||||
|
||||
const [store] = await remoteQuery(queryObject)
|
||||
res.status(200).json({ store })
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { result, errors } = await updateStoresWorkflow(req.scope).run({
|
||||
input: {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody as UpdateStoreDTO,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ store: result[0] })
|
||||
}
|
||||
42
packages/medusa/src/api-v2/admin/stores/middlewares.ts
Normal file
42
packages/medusa/src/api-v2/admin/stores/middlewares.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminGetStoresParams,
|
||||
AdminGetStoresStoreParams,
|
||||
AdminPostStoresStoreReq,
|
||||
} from "./validators"
|
||||
|
||||
export const adminStoreRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["ALL"],
|
||||
matcher: "/admin/stores*",
|
||||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/stores",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetStoresParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/stores/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetStoresStoreParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/stores/:id",
|
||||
middlewares: [transformBody(AdminPostStoresStoreReq)],
|
||||
},
|
||||
]
|
||||
22
packages/medusa/src/api-v2/admin/stores/query-config.ts
Normal file
22
packages/medusa/src/api-v2/admin/stores/query-config.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export const defaultAdminStoreRelations = []
|
||||
export const allowedAdminStoreRelations = []
|
||||
export const defaultAdminStoreFields = [
|
||||
"id",
|
||||
"name",
|
||||
"default_sales_channel_id",
|
||||
"default_region_id",
|
||||
"default_location_id",
|
||||
"metadata",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminStoreFields,
|
||||
defaultRelations: defaultAdminStoreRelations,
|
||||
allowedRelations: allowedAdminStoreRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
defaultLimit: 20,
|
||||
isList: true,
|
||||
}
|
||||
27
packages/medusa/src/api-v2/admin/stores/route.ts
Normal file
27
packages/medusa/src/api-v2/admin/stores/route.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { defaultAdminStoreFields } from "./query-config"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "store",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
order: req.listConfig.order,
|
||||
skip: req.listConfig.skip,
|
||||
take: req.listConfig.take,
|
||||
},
|
||||
fields: defaultAdminStoreFields,
|
||||
})
|
||||
|
||||
const { rows: stores, metadata } = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
stores,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
61
packages/medusa/src/api-v2/admin/stores/validators.ts
Normal file
61
packages/medusa/src/api-v2/admin/stores/validators.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Type } from "class-transformer"
|
||||
import { IsObject, IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
|
||||
export class AdminGetStoresStoreParams extends FindParams {}
|
||||
/**
|
||||
* Parameters used to filter and configure the pagination of the retrieved api keys.
|
||||
*/
|
||||
export class AdminGetStoresParams extends extendedFindParamsMixin({
|
||||
limit: 50,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* Search parameter for api keys.
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
id?: string | string[]
|
||||
|
||||
/**
|
||||
* Filter by title
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
name?: string | string[]
|
||||
|
||||
// Additional filters from BaseFilterable
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetStoresParams)
|
||||
$and?: AdminGetStoresParams[]
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetStoresParams)
|
||||
$or?: AdminGetStoresParams[]
|
||||
}
|
||||
|
||||
export class AdminPostStoresStoreReq {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
default_sales_channel_id?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
default_region_id?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
default_location_id?: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export class AdminDeleteStoresStoreReq {}
|
||||
@@ -6,6 +6,7 @@ import { adminCustomerRoutesMiddlewares } from "./admin/customers/middlewares"
|
||||
import { adminInviteRoutesMiddlewares } from "./admin/invites/middlewares"
|
||||
import { adminPromotionRoutesMiddlewares } from "./admin/promotions/middlewares"
|
||||
import { adminRegionRoutesMiddlewares } from "./admin/regions/middlewares"
|
||||
import { adminStoreRoutesMiddlewares } from "./admin/stores/middlewares"
|
||||
import { adminUserRoutesMiddlewares } from "./admin/users/middlewares"
|
||||
import { adminWorkflowsExecutionsMiddlewares } from "./admin/workflows-executions/middlewares"
|
||||
import { authRoutesMiddlewares } from "./auth/middlewares"
|
||||
@@ -31,5 +32,6 @@ export const config: MiddlewaresConfig = {
|
||||
...adminInviteRoutesMiddlewares,
|
||||
...adminApiKeyRoutesMiddlewares,
|
||||
...hooksRoutesMiddlewares,
|
||||
...adminStoreRoutesMiddlewares,
|
||||
],
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"default": "'Medusa Store'",
|
||||
"mappedType": "text"
|
||||
},
|
||||
"default_sales_channel_id": {
|
||||
@@ -70,11 +71,42 @@
|
||||
"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": [
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import { Migration } from "@mikro-orm/migrations"
|
||||
|
||||
export class InitialSetup20240226130829 extends Migration {
|
||||
async up(): Promise<void> {
|
||||
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"));'
|
||||
)
|
||||
}
|
||||
}
|
||||
48
packages/store/src/migrations/InitialSetup20240227075933.ts
Normal file
48
packages/store/src/migrations/InitialSetup20240227075933.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
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_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;`)
|
||||
this.addSql(
|
||||
`alter table "store" add column "deleted_at" timestamptz null;`
|
||||
)
|
||||
|
||||
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 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\',
|
||||
"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,4 +1,10 @@
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
DALUtils,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { DAL } from "@medusajs/types"
|
||||
|
||||
import {
|
||||
BeforeCreate,
|
||||
@@ -6,14 +12,27 @@ import {
|
||||
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
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
@Property({ columnType: "text", default: "Medusa Store" })
|
||||
name: string
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
@@ -35,6 +54,18 @@ export default class Store {
|
||||
})
|
||||
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")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export interface CreateStoreDTO {
|
||||
name: string
|
||||
name?: string
|
||||
default_sales_channel_id?: string
|
||||
default_region_id?: string
|
||||
default_location_id?: string
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { FindConfig } from "../common"
|
||||
import { RestoreReturn, SoftDeleteReturn } from "../dal"
|
||||
import { IModuleService } from "../modules-sdk"
|
||||
import { Context } from "../shared-context"
|
||||
import { FilterableStoreProps, StoreDTO } from "./common"
|
||||
@@ -210,4 +211,42 @@ export interface IStoreModuleService extends IModuleService {
|
||||
config?: FindConfig<StoreDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<[StoreDTO[], number]>
|
||||
|
||||
/**
|
||||
* This method soft deletes stores by their IDs.
|
||||
*
|
||||
* @param {string[]} storeIds - The list of IDs of stores to soft delete.
|
||||
* @param {SoftDeleteReturn<TReturnableLinkableKeys>} config - An object that is used to specify an entity's related entities that should be soft-deleted when the main entity is soft-deleted.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<void | Record<string, string[]>>} Resolves successfully when the stores are soft-deleted.
|
||||
*
|
||||
* @example
|
||||
* {example-code}
|
||||
*/
|
||||
softDelete<TReturnableLinkableKeys extends string = string>(
|
||||
storeIds: string[],
|
||||
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
|
||||
sharedContext?: Context
|
||||
): Promise<Record<string, string[]> | void>
|
||||
|
||||
/**
|
||||
* This method restores soft deleted stores by their IDs.
|
||||
*
|
||||
* @param {string[]} storeIds - The list of IDs of stores to restore.
|
||||
* @param {RestoreReturn<TReturnableLinkableKeys>} config - Configurations determining which relations to restore along with each of the stores. You can pass to its `returnLinkableKeys`
|
||||
* property any of the stores's relation attribute names, such as `currency`.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<void | Record<string, string[]>>} An object that includes the IDs of related records that were restored, such as the ID of associated currency.
|
||||
* The object's keys are the ID attribute names of the stores entity's relations, such as `currency_code`,
|
||||
* and its value is an array of strings, each being the ID of the record associated with the store through this relation,
|
||||
* such as the IDs of associated currency.
|
||||
*
|
||||
* @example
|
||||
* {example-code}
|
||||
*/
|
||||
restore<TReturnableLinkableKeys extends string = string>(
|
||||
storeIds: string[],
|
||||
config?: RestoreReturn<TReturnableLinkableKeys>,
|
||||
sharedContext?: Context
|
||||
): Promise<Record<string, string[]> | void>
|
||||
}
|
||||
|
||||
@@ -8752,7 +8752,7 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@medusajs/store@workspace:packages/store":
|
||||
"@medusajs/store@workspace:^, @medusajs/store@workspace:packages/store":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@medusajs/store@workspace:packages/store"
|
||||
dependencies:
|
||||
@@ -31719,6 +31719,7 @@ __metadata:
|
||||
"@medusajs/product": "workspace:^"
|
||||
"@medusajs/promotion": "workspace:^"
|
||||
"@medusajs/region": "workspace:^"
|
||||
"@medusajs/store": "workspace:^"
|
||||
"@medusajs/types": "workspace:^"
|
||||
"@medusajs/user": "workspace:^"
|
||||
"@medusajs/utils": "workspace:^"
|
||||
|
||||
Reference in New Issue
Block a user