Chore/rm main entity concept (#7709)
**What** Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate. This pr also includes various fixes in different modules Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
2895ccfba8
commit
48963f55ef
+78
-47
@@ -2,7 +2,7 @@ import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ApiKeyType } from "@medusajs/utils"
|
||||
import crypto from "crypto"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import {
|
||||
createPublishableKeyFixture,
|
||||
createSecretKeyFixture,
|
||||
@@ -33,12 +33,9 @@ const mockSecretKeyBytes = () => {
|
||||
})
|
||||
}
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IApiKeyModuleService>({
|
||||
moduleName: Modules.API_KEY,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IApiKeyModuleService>) => {
|
||||
testSuite: ({ service }) => {
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks()
|
||||
})
|
||||
@@ -47,7 +44,9 @@ moduleIntegrationTestRunner({
|
||||
describe("creating a publishable API key", () => {
|
||||
it("should create it successfully", async function () {
|
||||
mockPublishableKeyBytes()
|
||||
const apiKey = await service.create(createPublishableKeyFixture)
|
||||
const apiKey = await service.createApiKeys(
|
||||
createPublishableKeyFixture
|
||||
)
|
||||
|
||||
expect(apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -69,7 +68,7 @@ moduleIntegrationTestRunner({
|
||||
describe("creating a secret API key", () => {
|
||||
it("should get created successfully", async function () {
|
||||
mockSecretKeyBytes()
|
||||
const apiKey = await service.create(createSecretKeyFixture)
|
||||
const apiKey = await service.createApiKeys(createSecretKeyFixture)
|
||||
|
||||
expect(apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -89,14 +88,17 @@ moduleIntegrationTestRunner({
|
||||
|
||||
it("should only allow creating one active token", async function () {
|
||||
await expect(
|
||||
service.create([createSecretKeyFixture, createSecretKeyFixture])
|
||||
service.createApiKeys([
|
||||
createSecretKeyFixture,
|
||||
createSecretKeyFixture,
|
||||
])
|
||||
).rejects.toThrow(
|
||||
"You can only create one secret key at a time. You tried to create 2 secret keys."
|
||||
)
|
||||
|
||||
await service.create(createSecretKeyFixture)
|
||||
await service.createApiKeys(createSecretKeyFixture)
|
||||
const err = await service
|
||||
.create(createSecretKeyFixture)
|
||||
.createApiKeys(createSecretKeyFixture)
|
||||
.catch((e) => e)
|
||||
expect(err.message).toEqual(
|
||||
"You can only have one active secret key a time. Revoke or delete your existing key before creating a new one."
|
||||
@@ -104,7 +106,9 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should allow for at most two tokens, where one is revoked", async function () {
|
||||
const firstApiKey = await service.create(createSecretKeyFixture)
|
||||
const firstApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
await service.revoke(
|
||||
{ id: firstApiKey.id },
|
||||
{
|
||||
@@ -112,9 +116,9 @@ moduleIntegrationTestRunner({
|
||||
}
|
||||
)
|
||||
|
||||
await service.create(createSecretKeyFixture)
|
||||
await service.createApiKeys(createSecretKeyFixture)
|
||||
const err = await service
|
||||
.create(createSecretKeyFixture)
|
||||
.createApiKeys(createSecretKeyFixture)
|
||||
.catch((e) => e)
|
||||
expect(err.message).toEqual(
|
||||
"You can only have one active secret key a time. Revoke or delete your existing key before creating a new one."
|
||||
@@ -124,7 +128,9 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("revoking API keys", () => {
|
||||
it("should have the revoked at and revoked by set when a key is revoked", async function () {
|
||||
const firstApiKey = await service.create(createSecretKeyFixture)
|
||||
const firstApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
const revokedKey = await service.revoke(firstApiKey.id, {
|
||||
revoked_by: "test",
|
||||
})
|
||||
@@ -142,7 +148,7 @@ moduleIntegrationTestRunner({
|
||||
const hourInSec = 3600
|
||||
jest.useFakeTimers().setSystemTime(now)
|
||||
|
||||
const createdKey = await service.create(createSecretKeyFixture)
|
||||
const createdKey = await service.createApiKeys(createSecretKeyFixture)
|
||||
const revokedKey = await service.revoke(createdKey.id, {
|
||||
revoked_by: "test",
|
||||
revoke_in: hourInSec,
|
||||
@@ -159,17 +165,21 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should do nothing if the revokal list is empty", async function () {
|
||||
const firstApiKey = await service.create(createSecretKeyFixture)
|
||||
const firstApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
let revokedKeys = await service.revoke([])
|
||||
expect(revokedKeys).toHaveLength(0)
|
||||
|
||||
const apiKey = await service.retrieve(firstApiKey.id)
|
||||
const apiKey = await service.retrieveApiKey(firstApiKey.id)
|
||||
expect(apiKey.revoked_at).toBeFalsy()
|
||||
expect(apiKey.revoked_by).toBeFalsy()
|
||||
})
|
||||
|
||||
it("should not allow revoking an already revoked API key", async function () {
|
||||
const firstApiKey = await service.create(createSecretKeyFixture)
|
||||
const firstApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
await service.revoke(firstApiKey.id, {
|
||||
revoked_by: "test",
|
||||
})
|
||||
@@ -188,18 +198,22 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("updating an API key", () => {
|
||||
it("should update the name successfully", async function () {
|
||||
const createdApiKey = await service.create(createSecretKeyFixture)
|
||||
const createdApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
|
||||
const updatedApiKey = await service.update(createdApiKey.id, {
|
||||
const updatedApiKey = await service.updateApiKeys(createdApiKey.id, {
|
||||
title: "New Name",
|
||||
})
|
||||
expect(updatedApiKey.title).toEqual("New Name")
|
||||
})
|
||||
|
||||
it("should not reflect any updates on other fields", async function () {
|
||||
const createdApiKey = await service.create(createSecretKeyFixture)
|
||||
const createdApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
|
||||
const updatedApiKey = await service.update(createdApiKey.id, {
|
||||
const updatedApiKey = await service.updateApiKeys(createdApiKey.id, {
|
||||
title: createdApiKey.title,
|
||||
revoked_by: "test",
|
||||
revoked_at: new Date(),
|
||||
@@ -214,27 +228,34 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("deleting API keys", () => {
|
||||
it("should successfully delete existing api keys", async function () {
|
||||
const createdApiKeys = await service.create([
|
||||
const createdApiKeys = await service.createApiKeys([
|
||||
createPublishableKeyFixture,
|
||||
createSecretKeyFixture,
|
||||
])
|
||||
await service.delete([createdApiKeys[0].id, createdApiKeys[1].id])
|
||||
await service.deleteApiKeys([
|
||||
createdApiKeys[0].id,
|
||||
createdApiKeys[1].id,
|
||||
])
|
||||
|
||||
const apiKeysInDatabase = await service.list()
|
||||
const apiKeysInDatabase = await service.listApiKeys()
|
||||
expect(apiKeysInDatabase).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("authenticating with API keys", () => {
|
||||
it("should authenticate a secret key successfully", async function () {
|
||||
const createdApiKey = await service.create(createSecretKeyFixture)
|
||||
const createdApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
const authenticated = await service.authenticate(createdApiKey.token)
|
||||
|
||||
expect(authenticated).toBeTruthy()
|
||||
expect(authenticated.title).toEqual(createSecretKeyFixture.title)
|
||||
})
|
||||
it("should authenticate with a token to be revoked in the future", async function () {
|
||||
const createdApiKey = await service.create(createSecretKeyFixture)
|
||||
const createdApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
|
||||
// We simulate setting the revoked_at in the future here
|
||||
jest.useFakeTimers().setSystemTime(new Date().setFullYear(3000))
|
||||
@@ -249,7 +270,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should not authenticate a publishable key", async function () {
|
||||
const createdApiKey = await service.create(
|
||||
const createdApiKey = await service.createApiKeys(
|
||||
createPublishableKeyFixture
|
||||
)
|
||||
const authenticated = await service.authenticate(createdApiKey.token)
|
||||
@@ -257,13 +278,17 @@ moduleIntegrationTestRunner({
|
||||
expect(authenticated).toBeFalsy()
|
||||
})
|
||||
it("should not authenticate with a non-existent token", async function () {
|
||||
const createdApiKey = await service.create(createSecretKeyFixture)
|
||||
const createdApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
const authenticated = await service.authenticate("some-token")
|
||||
|
||||
expect(authenticated).toBeFalsy()
|
||||
})
|
||||
it("should not authenticate with a revoked token", async function () {
|
||||
const createdApiKey = await service.create(createSecretKeyFixture)
|
||||
const createdApiKey = await service.createApiKeys(
|
||||
createSecretKeyFixture
|
||||
)
|
||||
await service.revoke(createdApiKey.id, {
|
||||
revoked_by: "test",
|
||||
})
|
||||
@@ -275,22 +300,22 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("retrieving API keys", () => {
|
||||
it("should successfully return all existing api keys", async function () {
|
||||
await service.create([
|
||||
await service.createApiKeys([
|
||||
createPublishableKeyFixture,
|
||||
createSecretKeyFixture,
|
||||
])
|
||||
|
||||
const apiKeysInDatabase = await service.list()
|
||||
const apiKeysInDatabase = await service.listApiKeys()
|
||||
expect(apiKeysInDatabase).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should only return keys with matching token", async function () {
|
||||
const created = await service.create([
|
||||
const created = await service.createApiKeys([
|
||||
createPublishableKeyFixture,
|
||||
createPublishableKeyFixture,
|
||||
])
|
||||
|
||||
const apiKeysInDatabase = await service.list({
|
||||
const apiKeysInDatabase = await service.listApiKeys({
|
||||
token: created[0].token,
|
||||
})
|
||||
expect(apiKeysInDatabase).toHaveLength(1)
|
||||
@@ -298,55 +323,61 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should not return the token and salt for secret keys when listing", async function () {
|
||||
await service.create([createSecretKeyFixture])
|
||||
await service.createApiKeys([createSecretKeyFixture])
|
||||
|
||||
const apiKeysInDatabase = await service.list()
|
||||
const apiKeysInDatabase = await service.listApiKeys()
|
||||
expect(apiKeysInDatabase).toHaveLength(1)
|
||||
expect(apiKeysInDatabase[0].token).toBeFalsy()
|
||||
expect(apiKeysInDatabase[0].salt).toBeFalsy()
|
||||
})
|
||||
|
||||
it("should return the token for publishable keys when listing", async function () {
|
||||
await service.create([createPublishableKeyFixture])
|
||||
await service.createApiKeys([createPublishableKeyFixture])
|
||||
|
||||
const apiKeysInDatabase = await service.list()
|
||||
const apiKeysInDatabase = await service.listApiKeys()
|
||||
expect(apiKeysInDatabase).toHaveLength(1)
|
||||
expect(apiKeysInDatabase[0].token).toBeTruthy()
|
||||
expect(apiKeysInDatabase[0].salt).toBeFalsy()
|
||||
})
|
||||
|
||||
it("should not return the token and salt for secret keys when listing and counting", async function () {
|
||||
await service.create([createSecretKeyFixture])
|
||||
await service.createApiKeys([createSecretKeyFixture])
|
||||
|
||||
const [apiKeysInDatabase] = await service.listAndCount()
|
||||
const [apiKeysInDatabase] = await service.listAndCountApiKeys()
|
||||
expect(apiKeysInDatabase).toHaveLength(1)
|
||||
expect(apiKeysInDatabase[0].token).toBeFalsy()
|
||||
expect(apiKeysInDatabase[0].salt).toBeFalsy()
|
||||
})
|
||||
|
||||
it("should return the token for publishable keys when listing and counting", async function () {
|
||||
await service.create([createPublishableKeyFixture])
|
||||
await service.createApiKeys([createPublishableKeyFixture])
|
||||
|
||||
const [apiKeysInDatabase] = await service.listAndCount()
|
||||
const [apiKeysInDatabase] = await service.listAndCountApiKeys()
|
||||
expect(apiKeysInDatabase).toHaveLength(1)
|
||||
expect(apiKeysInDatabase[0].token).toBeTruthy()
|
||||
expect(apiKeysInDatabase[0].salt).toBeFalsy()
|
||||
})
|
||||
|
||||
it("should not return the token and salt for secret keys when retrieving", async function () {
|
||||
const [createdApiKey] = await service.create([createSecretKeyFixture])
|
||||
const [createdApiKey] = await service.createApiKeys([
|
||||
createSecretKeyFixture,
|
||||
])
|
||||
|
||||
const apiKeyInDatabase = await service.retrieve(createdApiKey.id)
|
||||
const apiKeyInDatabase = await service.retrieveApiKey(
|
||||
createdApiKey.id
|
||||
)
|
||||
expect(apiKeyInDatabase.token).toBeFalsy()
|
||||
expect(apiKeyInDatabase.salt).toBeFalsy()
|
||||
})
|
||||
|
||||
it("should return the token for publishable keys when retrieving", async function () {
|
||||
const [createdApiKey] = await service.create([
|
||||
const [createdApiKey] = await service.createApiKeys([
|
||||
createPublishableKeyFixture,
|
||||
])
|
||||
|
||||
const apiKeyInDatabase = await service.retrieve(createdApiKey.id)
|
||||
const apiKeyInDatabase = await service.retrieveApiKey(
|
||||
createdApiKey.id
|
||||
)
|
||||
expect(apiKeyInDatabase.token).toBeTruthy()
|
||||
expect(apiKeyInDatabase.salt).toBeFalsy()
|
||||
})
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-api-key-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
|
||||
export * from "./types"
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { ApiKeyModuleService } from "@services"
|
||||
|
||||
const moduleDefinition: ModuleExports = { service: ApiKeyModuleService }
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,31 +1,11 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import ApiKey from "./models/api-key"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const LinkableKeys: Record<string, string> = {
|
||||
api_key_id: ApiKey.name,
|
||||
}
|
||||
export const joinerConfig = defineJoinerConfig(Modules.API_KEY)
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.API_KEY,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["api_key", "api_keys"],
|
||||
args: { entity: ApiKey.name },
|
||||
},
|
||||
],
|
||||
} as ModuleJoinerConfig
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { ApiKeyModuleService } from "@services"
|
||||
|
||||
const service = ApiKeyModuleService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import * as Models from "@models"
|
||||
import { EOL } from "os"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-api-key-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
const run = ModulesSdkUtils.buildSeedScript({
|
||||
moduleName: Modules.API_KEY,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/../../migrations",
|
||||
seedHandler: async ({ manager, data }) => {
|
||||
// TODO: Add seed logic
|
||||
},
|
||||
})
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -27,7 +27,7 @@ import {
|
||||
isString,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
MedusaService,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
@@ -38,17 +38,14 @@ type InjectedDependencies = {
|
||||
apiKeyService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
ApiKeyTypes.ApiKeyDTO,
|
||||
{
|
||||
ApiKey: { dto: ApiKeyTypes.ApiKeyDTO }
|
||||
}
|
||||
>(ApiKey, {}, entityNameToLinkableKeysMap)
|
||||
export default class ApiKeyModuleService
|
||||
extends MedusaService<{
|
||||
ApiKey: { dto: ApiKeyTypes.ApiKeyDTO }
|
||||
}>({ ApiKey }, entityNameToLinkableKeysMap)
|
||||
implements IApiKeyModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly apiKeyService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
|
||||
protected readonly apiKeyService_: ModulesSdkTypes.IMedusaInternalService<ApiKey>
|
||||
|
||||
constructor(
|
||||
{ baseRepository, apiKeyService }: InjectedDependencies,
|
||||
@@ -63,22 +60,22 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
create(
|
||||
//@ts-expect-error
|
||||
createApiKeys(
|
||||
data: ApiKeyTypes.CreateApiKeyDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO[]>
|
||||
create(
|
||||
createApiKeys(
|
||||
data: ApiKeyTypes.CreateApiKeyDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async create(
|
||||
async createApiKeys(
|
||||
data: ApiKeyTypes.CreateApiKeyDTO | ApiKeyTypes.CreateApiKeyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO | ApiKeyTypes.ApiKeyDTO[]> {
|
||||
const [createdApiKeys, generatedTokens] = await this.create_(
|
||||
const [createdApiKeys, generatedTokens] = await this.createApiKeys_(
|
||||
Array.isArray(data) ? data : [data],
|
||||
sharedContext
|
||||
)
|
||||
@@ -102,10 +99,10 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async create_(
|
||||
protected async createApiKeys_(
|
||||
data: ApiKeyTypes.CreateApiKeyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], TokenDTO[]]> {
|
||||
): Promise<[ApiKey[], TokenDTO[]]> {
|
||||
await this.validateCreateApiKeys_(data, sharedContext)
|
||||
|
||||
const normalizedInput: CreateApiKeyDTO[] = []
|
||||
@@ -135,17 +132,17 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
return [createdApiKeys, generatedTokens]
|
||||
}
|
||||
|
||||
async upsert(
|
||||
async upsertApiKeys(
|
||||
data: ApiKeyTypes.UpsertApiKeyDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO[]>
|
||||
async upsert(
|
||||
async upsertApiKeys(
|
||||
data: ApiKeyTypes.UpsertApiKeyDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async upsert(
|
||||
async upsertApiKeys(
|
||||
data: ApiKeyTypes.UpsertApiKeyDTO | ApiKeyTypes.UpsertApiKeyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO | ApiKeyTypes.ApiKeyDTO[]> {
|
||||
@@ -161,7 +158,7 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
|
||||
if (forCreate.length) {
|
||||
const op = async () => {
|
||||
const [createdApiKeys, generatedTokens] = await this.create_(
|
||||
const [createdApiKeys, generatedTokens] = await this.createApiKeys_(
|
||||
forCreate,
|
||||
sharedContext
|
||||
)
|
||||
@@ -188,7 +185,7 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
|
||||
if (forUpdate.length) {
|
||||
const op = async () => {
|
||||
const updateResp = await this.update_(forUpdate, sharedContext)
|
||||
const updateResp = await this.updateApiKeys_(forUpdate, sharedContext)
|
||||
return await this.baseRepository_.serialize<ApiKeyTypes.ApiKeyDTO[]>(
|
||||
updateResp
|
||||
)
|
||||
@@ -201,19 +198,20 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
return Array.isArray(data) ? result : result[0]
|
||||
}
|
||||
|
||||
async update(
|
||||
//@ts-expect-error
|
||||
async updateApiKeys(
|
||||
id: string,
|
||||
data: ApiKeyTypes.UpdateApiKeyDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO>
|
||||
async update(
|
||||
async updateApiKeys(
|
||||
selector: FilterableApiKeyProps,
|
||||
data: ApiKeyTypes.UpdateApiKeyDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ApiKeyTypes.ApiKeyDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
async updateApiKeys(
|
||||
idOrSelector: string | FilterableApiKeyProps,
|
||||
data: ApiKeyTypes.UpdateApiKeyDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -224,7 +222,10 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const updatedApiKeys = await this.update_(normalizedInput, sharedContext)
|
||||
const updatedApiKeys = await this.updateApiKeys_(
|
||||
normalizedInput,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const serializedResponse = await this.baseRepository_.serialize<
|
||||
ApiKeyTypes.ApiKeyDTO[]
|
||||
@@ -236,10 +237,10 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
protected async updateApiKeys_(
|
||||
normalizedInput: UpdateApiKeyInput[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<ApiKey[]> {
|
||||
const updateRequest = normalizedInput.map((k) => ({
|
||||
id: k.id,
|
||||
title: k.title,
|
||||
@@ -253,7 +254,8 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieve(
|
||||
// @ts-expect-error
|
||||
async retrieveApiKey(
|
||||
id: string,
|
||||
config?: FindConfig<ApiKeyTypes.ApiKeyDTO>,
|
||||
sharedContext?: Context
|
||||
@@ -269,7 +271,8 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async list(
|
||||
//@ts-expect-error
|
||||
async listApiKeys(
|
||||
filters?: ApiKeyTypes.FilterableApiKeyProps,
|
||||
config?: FindConfig<ApiKeyTypes.ApiKeyDTO>,
|
||||
sharedContext?: Context
|
||||
@@ -289,7 +292,8 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCount(
|
||||
//@ts-expect-error
|
||||
async listAndCountApiKeys(
|
||||
filters?: ApiKeyTypes.FilterableApiKeyProps,
|
||||
config?: FindConfig<ApiKeyTypes.ApiKeyDTO>,
|
||||
sharedContext?: Context
|
||||
@@ -347,7 +351,7 @@ export default class ApiKeyModuleService<TEntity extends ApiKey = ApiKey>
|
||||
async revoke_(
|
||||
normalizedInput: RevokeApiKeyInput[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<ApiKey[]> {
|
||||
await this.validateRevokeApiKeys_(normalizedInput)
|
||||
|
||||
const updateRequest = normalizedInput.map((k) => {
|
||||
|
||||
@@ -32,5 +32,5 @@ export async function createAuthIdentities(
|
||||
},
|
||||
]
|
||||
): Promise<AuthIdentity[]> {
|
||||
return await service.create(userData)
|
||||
return await service.createAuthIdentities(userData)
|
||||
}
|
||||
|
||||
+29
-29
@@ -5,12 +5,9 @@ import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IAuthModuleService>({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
testSuite: ({ service }) => {
|
||||
describe("AuthModuleService - AuthIdentity", () => {
|
||||
beforeEach(async () => {
|
||||
await createAuthIdentities(service)
|
||||
@@ -18,7 +15,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("listAuthIdentities", () => {
|
||||
it("should list authIdentities", async () => {
|
||||
const authIdentities = await service.list(
|
||||
const authIdentities = await service.listAuthIdentities(
|
||||
{},
|
||||
{ relations: ["provider_identities"] }
|
||||
)
|
||||
@@ -43,7 +40,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should list authIdentities by id", async () => {
|
||||
const authIdentities = await service.list({
|
||||
const authIdentities = await service.listAuthIdentities({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
@@ -55,7 +52,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should list authIdentities by provider", async () => {
|
||||
const authIdentities = await service.list({
|
||||
const authIdentities = await service.listAuthIdentities({
|
||||
provider_identities: {
|
||||
provider: "manual",
|
||||
},
|
||||
@@ -74,10 +71,11 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("listAndCountAuthIdentities", () => {
|
||||
it("should list and count authIdentities", async () => {
|
||||
const [authIdentities, count] = await service.listAndCount(
|
||||
{},
|
||||
{ relations: ["provider_identities"] }
|
||||
)
|
||||
const [authIdentities, count] =
|
||||
await service.listAndCountAuthIdentities(
|
||||
{},
|
||||
{ relations: ["provider_identities"] }
|
||||
)
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(authIdentities).toEqual([
|
||||
@@ -100,9 +98,10 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should listAndCount authIdentities by provider_id", async () => {
|
||||
const [authIdentities, count] = await service.listAndCount({
|
||||
provider_identities: { provider: "manual" },
|
||||
})
|
||||
const [authIdentities, count] =
|
||||
await service.listAndCountAuthIdentities({
|
||||
provider_identities: { provider: "manual" },
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authIdentities).toEqual([
|
||||
@@ -120,7 +119,7 @@ moduleIntegrationTestRunner({
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authIdentity for the given id", async () => {
|
||||
const authIdentity = await service.retrieve(id)
|
||||
const authIdentity = await service.retrieveAuthIdentity(id)
|
||||
|
||||
expect(authIdentity).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -133,7 +132,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
await service.retrieveAuthIdentity("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -144,7 +143,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should not return an authIdentity with password hash", async () => {
|
||||
const authIdentity = await service.retrieve("test-id-1")
|
||||
const authIdentity = await service.retrieveAuthIdentity("test-id-1")
|
||||
|
||||
expect(authIdentity).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -162,7 +161,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
await service.retrieveAuthIdentity(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -171,7 +170,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return authIdentity based on config select param", async () => {
|
||||
const authIdentity = await service.retrieve(id, {
|
||||
const authIdentity = await service.retrieveAuthIdentity(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
@@ -185,9 +184,9 @@ moduleIntegrationTestRunner({
|
||||
const id = "test-id"
|
||||
|
||||
it("should delete the authIdentities given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
await service.deleteAuthIdentities([id])
|
||||
|
||||
const authIdentities = await service.list({
|
||||
const authIdentities = await service.listAuthIdentities({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
@@ -202,7 +201,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
await service.updateAuthIdentites([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
@@ -217,14 +216,14 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update authIdentity", async () => {
|
||||
await service.update([
|
||||
await service.updateAuthIdentites([
|
||||
{
|
||||
id,
|
||||
app_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authIdentity] = await service.list({ id: [id] })
|
||||
const [authIdentity] = await service.listAuthIdentities({ id: [id] })
|
||||
expect(authIdentity).toEqual(
|
||||
expect.objectContaining({
|
||||
app_metadata: { email: "test@email.com" },
|
||||
@@ -235,7 +234,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("createAuthIdentity", () => {
|
||||
it("should create a authIdentity successfully", async () => {
|
||||
await service.create([
|
||||
await service.createAuthIdentities([
|
||||
{
|
||||
id: "test",
|
||||
provider_identities: [
|
||||
@@ -247,9 +246,10 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const [authIdentity, count] = await service.listAndCount({
|
||||
id: ["test"],
|
||||
})
|
||||
const [authIdentity, count] =
|
||||
await service.listAndCountAuthIdentities({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(authIdentity[0]).toEqual(
|
||||
|
||||
@@ -27,7 +27,7 @@ moduleIntegrationTestRunner({
|
||||
testSuite: ({ service }: SuiteOptions<IAuthModuleService>) =>
|
||||
describe("Auth Module Service", () => {
|
||||
beforeEach(async () => {
|
||||
await service.create({
|
||||
await service.createAuthIdentities({
|
||||
provider_identities: [
|
||||
{
|
||||
entity_id: "test@admin.com",
|
||||
@@ -103,7 +103,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
})
|
||||
|
||||
const dbAuthIdentity = await service.retrieve(
|
||||
const dbAuthIdentity = await service.retrieveAuthIdentity(
|
||||
result.authIdentity?.id!,
|
||||
{ relations: ["provider_identities"] }
|
||||
)
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-auth-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { AuthModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
const moduleDefinition: ModuleExports = {
|
||||
service: AuthModuleService,
|
||||
loaders: [loadProviders] as any,
|
||||
}
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,31 +1,14 @@
|
||||
import { AuthIdentity } from "@models"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
export const LinkableKeys = {
|
||||
auth_identity_id: AuthIdentity.name,
|
||||
}
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
export const joinerConfig = defineJoinerConfig(Modules.AUTH, {
|
||||
entityQueryingConfig: [AuthIdentity],
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.AUTH,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: {
|
||||
name: ["auth_identity", "auth_identities"],
|
||||
args: {
|
||||
entity: AuthIdentity.name,
|
||||
},
|
||||
},
|
||||
}
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { AuthModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
const service = AuthModuleService
|
||||
const loaders = [loadProviders] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
InjectManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
MedusaService,
|
||||
} from "@medusajs/utils"
|
||||
import AuthProviderService from "./auth-provider"
|
||||
|
||||
@@ -28,25 +28,16 @@ type InjectedDependencies = {
|
||||
providerIdentityService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
authProviderService: AuthProviderService
|
||||
}
|
||||
|
||||
const generateMethodForModels = { AuthIdentity, ProviderIdentity }
|
||||
|
||||
export default class AuthModuleService<
|
||||
TAuthIdentity extends AuthIdentity = AuthIdentity,
|
||||
TProviderIdentity extends ProviderIdentity = ProviderIdentity
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
AuthTypes.AuthIdentityDTO,
|
||||
{
|
||||
AuthIdentity: { dto: AuthTypes.AuthIdentityDTO }
|
||||
ProviderIdentity: { dto: AuthTypes.ProviderIdentityDTO }
|
||||
}
|
||||
>(AuthIdentity, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class AuthModuleService
|
||||
extends MedusaService<{
|
||||
AuthIdentity: { dto: AuthTypes.AuthIdentityDTO }
|
||||
ProviderIdentity: { dto: AuthTypes.ProviderIdentityDTO }
|
||||
}>({ AuthIdentity, ProviderIdentity }, entityNameToLinkableKeysMap)
|
||||
implements AuthTypes.IAuthModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected authIdentityService_: ModulesSdkTypes.IMedusaInternalService<TAuthIdentity>
|
||||
protected providerIdentityService_: ModulesSdkTypes.IMedusaInternalService<TProviderIdentity>
|
||||
protected authIdentityService_: ModulesSdkTypes.IMedusaInternalService<AuthIdentity>
|
||||
protected providerIdentityService_: ModulesSdkTypes.IMedusaInternalService<ProviderIdentity>
|
||||
protected readonly authProviderService_: AuthProviderService
|
||||
|
||||
constructor(
|
||||
@@ -71,18 +62,19 @@ export default class AuthModuleService<
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
create(
|
||||
// @ts-expect-error
|
||||
createAuthIdentities(
|
||||
data: AuthTypes.CreateAuthIdentityDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<AuthTypes.AuthIdentityDTO[]>
|
||||
|
||||
create(
|
||||
createAuthIdentities(
|
||||
data: AuthTypes.CreateAuthIdentityDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<AuthTypes.AuthIdentityDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async create(
|
||||
async createAuthIdentities(
|
||||
data: AuthTypes.CreateAuthIdentityDTO[] | AuthTypes.CreateAuthIdentityDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<AuthTypes.AuthIdentityDTO | AuthTypes.AuthIdentityDTO[]> {
|
||||
@@ -99,19 +91,19 @@ export default class AuthModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
update(
|
||||
// TODO: Update to follow convention
|
||||
updateAuthIdentites(
|
||||
data: AuthTypes.UpdateAuthIdentityDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<AuthTypes.AuthIdentityDTO[]>
|
||||
|
||||
update(
|
||||
updateAuthIdentites(
|
||||
data: AuthTypes.UpdateAuthIdentityDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<AuthTypes.AuthIdentityDTO>
|
||||
|
||||
// TODO: should be pluralized, see convention about the methods naming or the abstract module service interface definition @engineering
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
async updateAuthIdentites(
|
||||
data: AuthTypes.UpdateAuthIdentityDTO | AuthTypes.UpdateAuthIdentityDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<AuthTypes.AuthIdentityDTO | AuthTypes.AuthIdentityDTO[]> {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"directory": "packages/cache-inmemory"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
"access": "public"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
|
||||
+95
-95
@@ -2,21 +2,18 @@ import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { BigNumber } from "@medusajs/utils"
|
||||
import { CheckConstraintViolationException } from "@mikro-orm/core"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<ICartModuleService>({
|
||||
moduleName: Modules.CART,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<ICartModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
describe("Cart Module Service", () => {
|
||||
describe("create", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.create([
|
||||
.createCarts([
|
||||
{
|
||||
email: "test@email.com",
|
||||
} as any,
|
||||
@@ -29,13 +26,13 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a cart successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [cart] = await service.list({ id: [createdCart.id] })
|
||||
const [cart] = await service.listCarts({ id: [createdCart.id] })
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -46,7 +43,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a cart with billing + shipping address successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
billing_address: {
|
||||
@@ -60,7 +57,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const [cart] = await service.list(
|
||||
const [cart] = await service.listCarts(
|
||||
{ id: [createdCart.id] },
|
||||
{ relations: ["billing_address", "shipping_address"] }
|
||||
)
|
||||
@@ -89,7 +86,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
billing_address_id: createdAddress.id,
|
||||
@@ -116,7 +113,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a cart with items", async () => {
|
||||
const createdCart = await service.create({
|
||||
const createdCart = await service.createCarts({
|
||||
currency_code: "eur",
|
||||
items: [
|
||||
{
|
||||
@@ -127,7 +124,7 @@ moduleIntegrationTestRunner({
|
||||
],
|
||||
})
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items"],
|
||||
})
|
||||
|
||||
@@ -146,7 +143,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create multiple carts with items", async () => {
|
||||
const createdCarts = await service.create([
|
||||
const createdCarts = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
items: [
|
||||
@@ -169,7 +166,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const carts = await service.list(
|
||||
const carts = await service.listCarts(
|
||||
{ id: createdCarts.map((c) => c.id) },
|
||||
{
|
||||
relations: ["items"],
|
||||
@@ -204,7 +201,7 @@ moduleIntegrationTestRunner({
|
||||
describe("update", () => {
|
||||
it("should throw an error if cart does not exist", async () => {
|
||||
const error = await service
|
||||
.update([
|
||||
.updateCarts([
|
||||
{
|
||||
id: "none-existing",
|
||||
},
|
||||
@@ -217,20 +214,20 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update a cart successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [updatedCart] = await service.update([
|
||||
const [updatedCart] = await service.updateCarts([
|
||||
{
|
||||
id: createdCart.id,
|
||||
email: "test@email.com",
|
||||
},
|
||||
])
|
||||
|
||||
const [cart] = await service.list({ id: [createdCart.id] })
|
||||
const [cart] = await service.listCarts({ id: [createdCart.id] })
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -242,20 +239,20 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update a cart with selector successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [updatedCart] = await service.update(
|
||||
const [updatedCart] = await service.updateCarts(
|
||||
{ id: createdCart.id },
|
||||
{
|
||||
email: "test@email.com",
|
||||
}
|
||||
)
|
||||
|
||||
const [cart] = await service.list({ id: [createdCart.id] })
|
||||
const [cart] = await service.listCarts({ id: [createdCart.id] })
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -267,17 +264,17 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update a cart with id successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const updatedCart = await service.update(createdCart.id, {
|
||||
const updatedCart = await service.updateCarts(createdCart.id, {
|
||||
email: "test@email.com",
|
||||
})
|
||||
|
||||
const [cart] = await service.list({ id: [createdCart.id] })
|
||||
const [cart] = await service.listCarts({ id: [createdCart.id] })
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -291,15 +288,15 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("delete", () => {
|
||||
it("should delete a cart successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
await service.delete([createdCart.id])
|
||||
await service.deleteCarts([createdCart.id])
|
||||
|
||||
const carts = await service.list({ id: [createdCart.id] })
|
||||
const carts = await service.listCarts({ id: [createdCart.id] })
|
||||
|
||||
expect(carts.length).toEqual(0)
|
||||
})
|
||||
@@ -367,7 +364,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("addLineItems", () => {
|
||||
it("should add a line item to cart succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -381,7 +378,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items"],
|
||||
})
|
||||
|
||||
@@ -396,7 +393,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple line items to cart succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -417,7 +414,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items"],
|
||||
})
|
||||
|
||||
@@ -440,13 +437,13 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple line items to multiple carts succesfully", async () => {
|
||||
let [eurCart] = await service.create([
|
||||
let [eurCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
let [usdCart] = await service.create([
|
||||
let [usdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "usd",
|
||||
},
|
||||
@@ -467,7 +464,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const carts = await service.list(
|
||||
const carts = await service.listCarts(
|
||||
{ id: [eurCart.id, usdCart.id] },
|
||||
{ relations: ["items"] }
|
||||
)
|
||||
@@ -501,7 +498,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should throw an error when required params are not passed adding to a single cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -522,7 +519,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should throw a generic error when required params are not passed using bulk add method", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -546,7 +543,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("updateLineItems", () => {
|
||||
it("should update a line item in cart succesfully with selector approach", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -574,7 +571,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update a line item in cart succesfully with id approach", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -599,7 +596,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update line items in carts succesfully with multi-selector approach", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -670,7 +667,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("removeLineItems", () => {
|
||||
it("should remove a line item succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -689,7 +686,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
await service.softDeleteLineItems([item.id])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items"],
|
||||
})
|
||||
|
||||
@@ -697,7 +694,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should remove multiple line items succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -718,7 +715,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
await service.softDeleteLineItems([item.id, item2.id])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items"],
|
||||
})
|
||||
|
||||
@@ -728,7 +725,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("addShippingMethods", () => {
|
||||
it("should add a shipping method to cart succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -741,7 +738,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods"],
|
||||
})
|
||||
|
||||
@@ -749,7 +746,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should throw when amount is negative", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -768,13 +765,13 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple shipping methods to multiple carts succesfully", async () => {
|
||||
let [eurCart] = await service.create([
|
||||
let [eurCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
let [usdCart] = await service.create([
|
||||
let [usdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "usd",
|
||||
},
|
||||
@@ -793,7 +790,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const carts = await service.list(
|
||||
const carts = await service.listCarts(
|
||||
{ id: [eurCart.id, usdCart.id] },
|
||||
{ relations: ["shipping_methods"] }
|
||||
)
|
||||
@@ -814,7 +811,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("removeShippingMethods", () => {
|
||||
it("should remove a line item succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -831,7 +828,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
await service.softDeleteShippingMethods([method.id])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods"],
|
||||
})
|
||||
|
||||
@@ -841,7 +838,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("setLineItemAdjustments", () => {
|
||||
it("should set line item adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -896,7 +893,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should replace line item adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -939,7 +936,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items.adjustments"],
|
||||
})
|
||||
|
||||
@@ -963,7 +960,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should remove all line item adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1000,7 +997,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
await service.setLineItemAdjustments(createdCart.id, [])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items.adjustments"],
|
||||
})
|
||||
|
||||
@@ -1018,7 +1015,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update line item adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1062,7 +1059,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items.adjustments"],
|
||||
})
|
||||
|
||||
@@ -1089,7 +1086,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("addLineItemAdjustments", () => {
|
||||
it("should add line item adjustments for items in a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1126,7 +1123,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple line item adjustments for multiple line items", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1180,12 +1177,12 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add line item adjustments for line items on multiple carts", async () => {
|
||||
const [cartOne] = await service.create([
|
||||
const [cartOne] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
const [cartTwo] = await service.create([
|
||||
const [cartTwo] = await service.createCarts([
|
||||
{
|
||||
currency_code: "usd",
|
||||
},
|
||||
@@ -1261,7 +1258,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("removeLineItemAdjustments", () => {
|
||||
it("should remove a line item succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1279,6 +1276,7 @@ moduleIntegrationTestRunner({
|
||||
createdCart.id,
|
||||
[
|
||||
{
|
||||
code: "50",
|
||||
item_id: item.id,
|
||||
amount: 50,
|
||||
},
|
||||
@@ -1299,7 +1297,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("setShippingMethodAdjustments", () => {
|
||||
it("should set shipping method adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1358,7 +1356,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should replace shipping method adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1403,7 +1401,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods.adjustments"],
|
||||
})
|
||||
|
||||
@@ -1428,7 +1426,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should remove all shipping method adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1467,7 +1465,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
await service.setShippingMethodAdjustments(createdCart.id, [])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods.adjustments"],
|
||||
})
|
||||
|
||||
@@ -1485,7 +1483,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update shipping method adjustments for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1530,7 +1528,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods.adjustments"],
|
||||
})
|
||||
|
||||
@@ -1557,7 +1555,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("addShippingMethodAdjustments", () => {
|
||||
it("should add shipping method adjustments in a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1596,7 +1594,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple shipping method adjustments for multiple shipping methods", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1654,12 +1652,12 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add shipping method adjustments for shipping methods on multiple carts", async () => {
|
||||
const [cartOne] = await service.create([
|
||||
const [cartOne] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
const [cartTwo] = await service.create([
|
||||
const [cartTwo] = await service.createCarts([
|
||||
{
|
||||
currency_code: "usd",
|
||||
},
|
||||
@@ -1738,13 +1736,13 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should throw if shipping method is not associated with cart", async () => {
|
||||
const [cartOne] = await service.create([
|
||||
const [cartOne] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [cartTwo] = await service.create([
|
||||
const [cartTwo] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1778,7 +1776,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("removeShippingMethodAdjustments", () => {
|
||||
it("should remove a shipping method succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1816,7 +1814,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("setLineItemTaxLines", () => {
|
||||
it("should set line item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1868,7 +1866,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should replace line item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1908,7 +1906,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items.tax_lines"],
|
||||
})
|
||||
|
||||
@@ -1932,7 +1930,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should remove all line item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -1966,7 +1964,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
await service.setLineItemTaxLines(createdCart.id, [])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items.tax_lines"],
|
||||
})
|
||||
|
||||
@@ -1984,7 +1982,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update line item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -2025,7 +2023,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items.tax_lines"],
|
||||
})
|
||||
|
||||
@@ -2050,7 +2048,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should remove, update, and create line item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -2110,7 +2108,7 @@ moduleIntegrationTestRunner({
|
||||
// remove: should remove the initial tax line for itemOne
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["items.tax_lines"],
|
||||
})
|
||||
|
||||
@@ -2142,7 +2140,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("addLineItemAdjustments", () => {
|
||||
it("should add line item tax lines for items in a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -2176,7 +2174,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple line item tax lines for multiple line items", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -2227,12 +2225,12 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add line item tax lines for line items on multiple carts", async () => {
|
||||
const [cartOne] = await service.create([
|
||||
const [cartOne] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
const [cartTwo] = await service.create([
|
||||
const [cartTwo] = await service.createCarts([
|
||||
{
|
||||
currency_code: "usd",
|
||||
},
|
||||
@@ -2308,7 +2306,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("removeLineItemAdjustments", () => {
|
||||
it("should remove line item tax line succesfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -2344,7 +2342,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should calculate totals of a cart", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
@@ -2386,7 +2384,9 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, { select: ["total"] })
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
select: ["total"],
|
||||
})
|
||||
expect(cart.total).toBeInstanceOf(BigNumber)
|
||||
|
||||
const asJson = JSON.parse(JSON.stringify(cart))
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-cart-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { CartModuleService } from "./services"
|
||||
|
||||
const service = CartModuleService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,98 +1,11 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import {
|
||||
Address,
|
||||
Cart,
|
||||
LineItem,
|
||||
LineItemAdjustment,
|
||||
LineItemTaxLine,
|
||||
ShippingMethod,
|
||||
ShippingMethodAdjustment,
|
||||
ShippingMethodTaxLine,
|
||||
} from "@models"
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const LinkableKeys = {
|
||||
cart_id: Cart.name,
|
||||
line_item_id: LineItem.name,
|
||||
shipping_method_id: ShippingMethod.name,
|
||||
address_id: Address.name,
|
||||
line_item_adjustment_id: LineItemAdjustment.name,
|
||||
shipping_method_adjustment_id: ShippingMethodAdjustment.name,
|
||||
line_item_tax_line_id: LineItemTaxLine.name,
|
||||
shipping_method_tax_line_id: ShippingMethodTaxLine.name,
|
||||
}
|
||||
export const joinerConfig = defineJoinerConfig(Modules.CART)
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.CART,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["cart", "carts"],
|
||||
args: {
|
||||
entity: Cart.name,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["line_item", "line_items"],
|
||||
args: {
|
||||
entity: LineItem.name,
|
||||
methodSuffix: "LineItems",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["shipping_method", "shipping_methods"],
|
||||
args: {
|
||||
entity: ShippingMethod.name,
|
||||
methodSuffix: "ShippingMethods",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["address", "addresses"],
|
||||
args: {
|
||||
entity: Address.name,
|
||||
methodSuffix: "Addresses",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["line_item_adjustment", "line_item_adjustments"],
|
||||
args: {
|
||||
entity: LineItemAdjustment.name,
|
||||
methodSuffix: "LineItemAdjustments",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["shipping_method_adjustment", "shipping_method_adjustments"],
|
||||
args: {
|
||||
entity: ShippingMethodAdjustment.name,
|
||||
methodSuffix: "ShippingMethodAdjustments",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["line_item_tax_line", "line_item_tax_lines"],
|
||||
args: {
|
||||
entity: LineItemTaxLine.name,
|
||||
methodSuffix: "LineItemTaxLines",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["shipping_method_tax_line", "shipping_method_tax_lines"],
|
||||
args: {
|
||||
entity: ShippingMethodTaxLine.name,
|
||||
methodSuffix: "ShippingMethodTaxLines",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { CartModuleService } from "./services"
|
||||
|
||||
const service = CartModuleService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils";
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { EOL } from "os"
|
||||
import { run } from "../seed"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-cart-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -1,63 +0,0 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as CartModels from "@models"
|
||||
import { EOL } from "os"
|
||||
import { resolve } from "path"
|
||||
|
||||
export async function run({
|
||||
options,
|
||||
logger,
|
||||
path,
|
||||
}: Partial<
|
||||
Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
>
|
||||
> & {
|
||||
path: string
|
||||
}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
logger.info(`Loading seed data from ${path}...`)
|
||||
|
||||
const { cartData } = await import(
|
||||
resolve(process.cwd(), path)
|
||||
).catch((e) => {
|
||||
logger?.error(
|
||||
`Failed to load seed data from ${path}. Please, provide a relative path and check that you export the following: cartData.${EOL}${e}`
|
||||
)
|
||||
throw e
|
||||
})
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
||||
Modules.CART,
|
||||
options
|
||||
)!
|
||||
const entities = Object.values(
|
||||
CartModels
|
||||
) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
const manager = orm.em.fork()
|
||||
|
||||
try {
|
||||
logger.info("Seeding cart data..")
|
||||
|
||||
// TODO: implement cart seed data
|
||||
// await createCarts(manager, cartsData)
|
||||
} catch (e) {
|
||||
logger.error(
|
||||
`Failed to insert the seed data in the PostgreSQL database ${dbData.clientUrl}.${EOL}${e}`
|
||||
)
|
||||
}
|
||||
|
||||
await orm.close(true)
|
||||
}
|
||||
@@ -54,6 +54,7 @@ type InjectedDependencies = {
|
||||
}
|
||||
|
||||
const generateMethodForModels = {
|
||||
Cart,
|
||||
Address,
|
||||
LineItem,
|
||||
LineItemAdjustment,
|
||||
@@ -63,39 +64,28 @@ const generateMethodForModels = {
|
||||
ShippingMethodTaxLine,
|
||||
}
|
||||
|
||||
export default class CartModuleService<
|
||||
TCart extends Cart = Cart,
|
||||
TAddress extends Address = Address,
|
||||
TLineItem extends LineItem = LineItem,
|
||||
TLineItemAdjustment extends LineItemAdjustment = LineItemAdjustment,
|
||||
TLineItemTaxLine extends LineItemTaxLine = LineItemTaxLine,
|
||||
TShippingMethodAdjustment extends ShippingMethodAdjustment = ShippingMethodAdjustment,
|
||||
TShippingMethodTaxLine extends ShippingMethodTaxLine = ShippingMethodTaxLine,
|
||||
TShippingMethod extends ShippingMethod = ShippingMethod
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
CartTypes.CartDTO,
|
||||
{
|
||||
Address: { dto: CartTypes.CartAddressDTO }
|
||||
LineItem: { dto: CartTypes.CartLineItemDTO }
|
||||
LineItemAdjustment: { dto: CartTypes.LineItemAdjustmentDTO }
|
||||
LineItemTaxLine: { dto: CartTypes.LineItemTaxLineDTO }
|
||||
ShippingMethod: { dto: CartTypes.CartShippingMethodDTO }
|
||||
ShippingMethodAdjustment: { dto: CartTypes.ShippingMethodAdjustmentDTO }
|
||||
ShippingMethodTaxLine: { dto: CartTypes.ShippingMethodTaxLineDTO }
|
||||
}
|
||||
>(Cart, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class CartModuleService
|
||||
extends ModulesSdkUtils.MedusaService<{
|
||||
Cart: { dto: CartTypes.CartDTO }
|
||||
Address: { dto: CartTypes.CartAddressDTO }
|
||||
LineItem: { dto: CartTypes.CartLineItemDTO }
|
||||
LineItemAdjustment: { dto: CartTypes.LineItemAdjustmentDTO }
|
||||
LineItemTaxLine: { dto: CartTypes.LineItemTaxLineDTO }
|
||||
ShippingMethod: { dto: CartTypes.CartShippingMethodDTO }
|
||||
ShippingMethodAdjustment: { dto: CartTypes.ShippingMethodAdjustmentDTO }
|
||||
ShippingMethodTaxLine: { dto: CartTypes.ShippingMethodTaxLineDTO }
|
||||
}>(generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements ICartModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected cartService_: ModulesSdkTypes.IMedusaInternalService<TCart>
|
||||
protected addressService_: ModulesSdkTypes.IMedusaInternalService<TAddress>
|
||||
protected lineItemService_: ModulesSdkTypes.IMedusaInternalService<TLineItem>
|
||||
protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethodAdjustment>
|
||||
protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethod>
|
||||
protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<TLineItemAdjustment>
|
||||
protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TLineItemTaxLine>
|
||||
protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethodTaxLine>
|
||||
protected cartService_: ModulesSdkTypes.IMedusaInternalService<Cart>
|
||||
protected addressService_: ModulesSdkTypes.IMedusaInternalService<Address>
|
||||
protected lineItemService_: ModulesSdkTypes.IMedusaInternalService<LineItem>
|
||||
protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<ShippingMethodAdjustment>
|
||||
protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService<ShippingMethod>
|
||||
protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<LineItemAdjustment>
|
||||
protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService<LineItemTaxLine>
|
||||
protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService<ShippingMethodTaxLine>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -189,7 +179,8 @@ export default class CartModuleService<
|
||||
})
|
||||
}
|
||||
|
||||
async retrieve(
|
||||
// @ts-expect-error
|
||||
async retrieveCart(
|
||||
id: string,
|
||||
config?: FindConfig<any> | undefined,
|
||||
sharedContext?: Context | undefined
|
||||
@@ -197,7 +188,7 @@ export default class CartModuleService<
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const cart = await super.retrieve(id, config, sharedContext)
|
||||
const cart = await super.retrieveCart(id, config, sharedContext)
|
||||
|
||||
if (includeTotals) {
|
||||
createRawPropertiesFromBigNumber(decorateCartTotals(cart))
|
||||
@@ -206,7 +197,8 @@ export default class CartModuleService<
|
||||
return cart
|
||||
}
|
||||
|
||||
async list(
|
||||
// @ts-expect-error
|
||||
async listCarts(
|
||||
filters?: any,
|
||||
config?: FindConfig<any> | undefined,
|
||||
sharedContext?: Context | undefined
|
||||
@@ -214,7 +206,7 @@ export default class CartModuleService<
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const carts = await super.list(filters, config, sharedContext)
|
||||
const carts = await super.listCarts(filters, config, sharedContext)
|
||||
|
||||
if (includeTotals) {
|
||||
carts.forEach((cart) => {
|
||||
@@ -225,7 +217,8 @@ export default class CartModuleService<
|
||||
return carts
|
||||
}
|
||||
|
||||
async listAndCount(
|
||||
// @ts-expect-error
|
||||
async listAndCountCarts(
|
||||
filters?: any,
|
||||
config?: FindConfig<any> | undefined,
|
||||
sharedContext?: Context | undefined
|
||||
@@ -233,7 +226,7 @@ export default class CartModuleService<
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const [carts, count] = await super.listAndCount(
|
||||
const [carts, count] = await super.listAndCountCarts(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
@@ -248,26 +241,27 @@ export default class CartModuleService<
|
||||
return [carts, count]
|
||||
}
|
||||
|
||||
async create(
|
||||
// @ts-expect-error
|
||||
async createCarts(
|
||||
data: CartTypes.CreateCartDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO[]>
|
||||
|
||||
async create(
|
||||
async createCarts(
|
||||
data: CartTypes.CreateCartDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async create(
|
||||
async createCarts(
|
||||
data: CartTypes.CreateCartDTO[] | CartTypes.CreateCartDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.CartDTO[] | CartTypes.CartDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const carts = await this.create_(input, sharedContext)
|
||||
const carts = await this.createCarts_(input, sharedContext)
|
||||
|
||||
const result = await this.list(
|
||||
const result = await this.listCarts(
|
||||
{ id: carts.map((p) => p!.id) },
|
||||
{
|
||||
relations: ["shipping_address", "billing_address"],
|
||||
@@ -281,7 +275,7 @@ export default class CartModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async create_(
|
||||
protected async createCarts_(
|
||||
data: CartTypes.CreateCartDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
@@ -311,20 +305,23 @@ export default class CartModuleService<
|
||||
return createdCarts
|
||||
}
|
||||
|
||||
async update(data: CartTypes.UpdateCartDTO[]): Promise<CartTypes.CartDTO[]>
|
||||
async update(
|
||||
// @ts-expect-error
|
||||
async updateCarts(
|
||||
data: CartTypes.UpdateCartDTO[]
|
||||
): Promise<CartTypes.CartDTO[]>
|
||||
async updateCarts(
|
||||
cartId: string,
|
||||
data: CartTypes.UpdateCartDataDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO>
|
||||
async update(
|
||||
async updateCarts(
|
||||
selector: Partial<CartTypes.CartDTO>,
|
||||
data: CartTypes.UpdateCartDataDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
async updateCarts(
|
||||
dataOrIdOrSelector:
|
||||
| CartTypes.UpdateCartDTO[]
|
||||
| string
|
||||
@@ -332,7 +329,11 @@ export default class CartModuleService<
|
||||
data?: CartTypes.UpdateCartDataDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.CartDTO[] | CartTypes.CartDTO> {
|
||||
const result = await this.update_(dataOrIdOrSelector, data, sharedContext)
|
||||
const result = await this.updateCarts_(
|
||||
dataOrIdOrSelector,
|
||||
data,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const serializedResult = await this.baseRepository_.serialize<
|
||||
CartTypes.CartDTO[]
|
||||
@@ -344,7 +345,7 @@ export default class CartModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
protected async updateCarts_(
|
||||
dataOrIdOrSelector:
|
||||
| CartTypes.UpdateCartDTO[]
|
||||
| string
|
||||
@@ -428,7 +429,11 @@ export default class CartModuleService<
|
||||
items: CartTypes.CreateLineItemDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<LineItem[]> {
|
||||
const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext)
|
||||
const cart = await this.retrieveCart(
|
||||
cartId,
|
||||
{ select: ["id"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const toUpdate: CreateLineItemDTO[] = items.map((item) => {
|
||||
return {
|
||||
@@ -666,7 +671,11 @@ export default class CartModuleService<
|
||||
data: CartTypes.CreateShippingMethodForSingleCartDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ShippingMethod[]> {
|
||||
const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext)
|
||||
const cart = await this.retrieveCart(
|
||||
cartId,
|
||||
{ select: ["id"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const methods = data.map((method) => {
|
||||
return {
|
||||
@@ -712,7 +721,7 @@ export default class CartModuleService<
|
||||
): Promise<CartTypes.LineItemAdjustmentDTO[]> {
|
||||
let addedAdjustments: LineItemAdjustment[] = []
|
||||
if (isString(cartIdOrData)) {
|
||||
const cart = await this.retrieve(
|
||||
const cart = await this.retrieveCart(
|
||||
cartIdOrData,
|
||||
{ select: ["id"], relations: ["items"] },
|
||||
sharedContext
|
||||
@@ -758,7 +767,7 @@ export default class CartModuleService<
|
||||
)[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.LineItemAdjustmentDTO[]> {
|
||||
const cart = await this.retrieve(
|
||||
const cart = await this.retrieveCart(
|
||||
cartId,
|
||||
{ select: ["id"], relations: ["items.adjustments"] },
|
||||
sharedContext
|
||||
@@ -813,7 +822,7 @@ export default class CartModuleService<
|
||||
)[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.ShippingMethodAdjustmentDTO[]> {
|
||||
const cart = await this.retrieve(
|
||||
const cart = await this.retrieveCart(
|
||||
cartId,
|
||||
{ select: ["id"], relations: ["shipping_methods.adjustments"] },
|
||||
sharedContext
|
||||
@@ -887,7 +896,7 @@ export default class CartModuleService<
|
||||
> {
|
||||
let addedAdjustments: ShippingMethodAdjustment[] = []
|
||||
if (isString(cartIdOrData)) {
|
||||
const cart = await this.retrieve(
|
||||
const cart = await this.retrieveCart(
|
||||
cartIdOrData,
|
||||
{ select: ["id"], relations: ["shipping_methods"] },
|
||||
sharedContext
|
||||
@@ -961,7 +970,7 @@ export default class CartModuleService<
|
||||
let addedTaxLines: LineItemTaxLine[]
|
||||
if (isString(cartIdOrData)) {
|
||||
// existence check
|
||||
await this.retrieve(cartIdOrData, { select: ["id"] }, sharedContext)
|
||||
await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext)
|
||||
|
||||
const lines = Array.isArray(taxLines) ? taxLines : [taxLines]
|
||||
|
||||
@@ -1000,7 +1009,7 @@ export default class CartModuleService<
|
||||
)[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.LineItemTaxLineDTO[]> {
|
||||
const cart = await this.retrieve(
|
||||
const cart = await this.retrieveCart(
|
||||
cartId,
|
||||
{ select: ["id"], relations: ["items.tax_lines"] },
|
||||
sharedContext
|
||||
@@ -1077,7 +1086,7 @@ export default class CartModuleService<
|
||||
let addedTaxLines: ShippingMethodTaxLine[]
|
||||
if (isString(cartIdOrData)) {
|
||||
// existence check
|
||||
await this.retrieve(cartIdOrData, { select: ["id"] }, sharedContext)
|
||||
await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext)
|
||||
|
||||
const lines = Array.isArray(taxLines) ? taxLines : [taxLines]
|
||||
|
||||
@@ -1116,7 +1125,7 @@ export default class CartModuleService<
|
||||
)[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.ShippingMethodTaxLineDTO[]> {
|
||||
const cart = await this.retrieve(
|
||||
const cart = await this.retrieveCart(
|
||||
cartId,
|
||||
{ select: ["id"], relations: ["shipping_methods.tax_lines"] },
|
||||
sharedContext
|
||||
|
||||
+34
-36
@@ -1,19 +1,19 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<ICurrencyModuleService>({
|
||||
moduleName: Modules.CURRENCY,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<ICurrencyModuleService>) => {
|
||||
testSuite: ({ service }) => {
|
||||
describe("Currency Module Service", () => {
|
||||
describe("list", () => {
|
||||
it("list currencies", async () => {
|
||||
const currenciesResult = await service.list({}, { take: null })
|
||||
const currenciesResult = await service.listCurrencies(
|
||||
{},
|
||||
{ take: null }
|
||||
)
|
||||
expect(currenciesResult).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
@@ -31,7 +31,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("list currencies by code", async () => {
|
||||
const currenciesResult = await service.list(
|
||||
const currenciesResult = await service.listCurrencies(
|
||||
{ code: ["usd"] },
|
||||
{ take: null }
|
||||
)
|
||||
@@ -45,7 +45,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("list currencies by code regardless of case-sensitivity", async () => {
|
||||
const currenciesResult = await service.list(
|
||||
const currenciesResult = await service.listCurrencies(
|
||||
{ code: ["Usd"] },
|
||||
{ take: null }
|
||||
)
|
||||
@@ -59,12 +59,10 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
describe("listAndCountCurrenciesCurrencies", () => {
|
||||
it("should return currencies and count", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{},
|
||||
{ take: null }
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies({}, { take: null })
|
||||
|
||||
expect(count).toEqual(120)
|
||||
expect(currenciesResult).toEqual(
|
||||
@@ -82,12 +80,13 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currencies and count when filtered", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{
|
||||
code: ["usd"],
|
||||
},
|
||||
{ take: null }
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies(
|
||||
{
|
||||
code: ["usd"],
|
||||
},
|
||||
{ take: null }
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(currenciesResult).toEqual([
|
||||
@@ -99,10 +98,8 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currencies and count when using skip and take", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 5, take: 1 }
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies({}, { skip: 5, take: 1 })
|
||||
|
||||
expect(count).toEqual(120)
|
||||
expect(currenciesResult).toEqual([
|
||||
@@ -114,13 +111,14 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["code", "rounding"],
|
||||
}
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["code", "rounding"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(currenciesResult))
|
||||
|
||||
@@ -140,7 +138,7 @@ moduleIntegrationTestRunner({
|
||||
const name = "US Dollar"
|
||||
|
||||
it("should return currency for the given code", async () => {
|
||||
const currency = await service.retrieve(code)
|
||||
const currency = await service.retrieveCurrency(code)
|
||||
|
||||
expect(currency).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -150,7 +148,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currency for the given code in a case-insensitive manner", async () => {
|
||||
const currency = await service.retrieve(code.toUpperCase())
|
||||
const currency = await service.retrieveCurrency(code.toUpperCase())
|
||||
|
||||
expect(currency).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -163,7 +161,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
await service.retrieveCurrency("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -177,7 +175,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
await service.retrieveCurrency(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -186,7 +184,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currency based on config select param", async () => {
|
||||
const currency = await service.retrieve(code, {
|
||||
const currency = await service.retrieveCurrency(code, {
|
||||
select: ["code", "name"],
|
||||
})
|
||||
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-currency-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { CurrencyModuleService } from "@services"
|
||||
import initialDataLoader from "./loaders/initial-data"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
|
||||
export * from "./types"
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
const service = CurrencyModuleService
|
||||
const loaders = [initialDataLoader]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,33 +1,11 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import Currency from "./models/currency"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const LinkableKeys: Record<string, string> = {
|
||||
code: Currency.name,
|
||||
currency_code: Currency.name,
|
||||
default_currency_code: Currency.name,
|
||||
}
|
||||
export const joinerConfig = defineJoinerConfig(Modules.CURRENCY)
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.CURRENCY,
|
||||
primaryKeys: ["code"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["currency", "currencies"],
|
||||
args: { entity: Currency.name },
|
||||
},
|
||||
],
|
||||
} as ModuleJoinerConfig
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { CurrencyModuleService } from "@services"
|
||||
import initialDataLoader from "./loaders/initial-data"
|
||||
|
||||
const service = CurrencyModuleService
|
||||
const loaders = [initialDataLoader]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import * as Models from "@models"
|
||||
import { EOL } from "os"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-currency-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
const run = ModulesSdkUtils.buildSeedScript({
|
||||
moduleName: Modules.CURRENCY,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/../../migrations",
|
||||
seedHandler: async ({ manager, data }) => {
|
||||
// TODO: Add seed logic
|
||||
},
|
||||
})
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -10,29 +10,24 @@ import {
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
} from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
import { Currency } from "@models"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
|
||||
const generateMethodForModels = {}
|
||||
import { MedusaService } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
currencyService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
export default class CurrencyModuleService<TEntity extends Currency = Currency>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
CurrencyTypes.CurrencyDTO,
|
||||
{
|
||||
Currency: { dto: CurrencyTypes.CurrencyDTO }
|
||||
}
|
||||
>(Currency, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class CurrencyModuleService
|
||||
extends MedusaService<{
|
||||
Currency: { dto: CurrencyTypes.CurrencyDTO }
|
||||
}>({ Currency }, entityNameToLinkableKeysMap)
|
||||
implements ICurrencyModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
|
||||
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<Currency>
|
||||
|
||||
constructor(
|
||||
{ baseRepository, currencyService }: InjectedDependencies,
|
||||
@@ -48,36 +43,39 @@ export default class CurrencyModuleService<TEntity extends Currency = Currency>
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
retrieve(
|
||||
// @ts-expect-error
|
||||
async retrieveCurrency(
|
||||
code: string,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<CurrencyTypes.CurrencyDTO> {
|
||||
return this.currencyService_.retrieve(
|
||||
code?.toLowerCase(),
|
||||
return await super.retrieveCurrency(
|
||||
CurrencyModuleService.normalizeFilters({ code: [code] })!.code![0],
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
list(
|
||||
// @ts-expect-error
|
||||
async listCurrencies(
|
||||
filters?: FilterableCurrencyProps,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<CurrencyTypes.CurrencyDTO[]> {
|
||||
return this.currencyService_.list(
|
||||
return await super.listCurrencies(
|
||||
CurrencyModuleService.normalizeFilters(filters),
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
listAndCount(
|
||||
// @ts-expect-error
|
||||
async listAndCountCurrencies(
|
||||
filters?: FilterableCurrencyProps,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<[CurrencyTypes.CurrencyDTO[], number]> {
|
||||
return this.currencyService_.listAndCount(
|
||||
return await super.listAndCountCurrencies(
|
||||
CurrencyModuleService.normalizeFilters(filters),
|
||||
config,
|
||||
sharedContext
|
||||
|
||||
+133
-119
@@ -1,15 +1,12 @@
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
moduleName: Modules.CUSTOMER,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<ICustomerModuleService>) => {
|
||||
testSuite: ({ service }) => {
|
||||
describe("Customer Module Service", () => {
|
||||
describe("create", () => {
|
||||
it("should create a single customer", async () => {
|
||||
@@ -22,7 +19,7 @@ moduleIntegrationTestRunner({
|
||||
created_by: "admin",
|
||||
metadata: { membership: "gold" },
|
||||
}
|
||||
const customer = await service.create(customerData)
|
||||
const customer = await service.createCustomers(customerData)
|
||||
|
||||
expect(customer).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -52,7 +49,7 @@ moduleIntegrationTestRunner({
|
||||
...customerData,
|
||||
has_account: true,
|
||||
}
|
||||
const [customer, customer2] = await service.create([
|
||||
const [customer, customer2] = await service.createCustomers([
|
||||
customerData,
|
||||
customerData2,
|
||||
])
|
||||
@@ -96,7 +93,7 @@ moduleIntegrationTestRunner({
|
||||
}
|
||||
|
||||
const err = await service
|
||||
.create([customerData, customerData])
|
||||
.createCustomers([customerData, customerData])
|
||||
.catch((err) => err)
|
||||
|
||||
expect(err.message).toBe(
|
||||
@@ -117,7 +114,7 @@ moduleIntegrationTestRunner({
|
||||
}
|
||||
|
||||
const err = await service
|
||||
.create([customerData, customerData])
|
||||
.createCustomers([customerData, customerData])
|
||||
.catch((err) => err)
|
||||
|
||||
expect(err.message).toBe(
|
||||
@@ -144,7 +141,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
],
|
||||
}
|
||||
const customer = await service.create(customerData)
|
||||
const customer = await service.createCustomers(customerData)
|
||||
|
||||
const [address] = await service.listAddresses({
|
||||
customer_id: customer.id,
|
||||
@@ -192,7 +189,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
],
|
||||
}
|
||||
await expect(service.create(customerData)).rejects.toThrow(
|
||||
await expect(service.createCustomers(customerData)).rejects.toThrow(
|
||||
/Customer address with customer_id: .*? already exists./
|
||||
)
|
||||
})
|
||||
@@ -216,7 +213,7 @@ moduleIntegrationTestRunner({
|
||||
metadata: { membership: "silver" },
|
||||
},
|
||||
]
|
||||
const customer = await service.create(customersData)
|
||||
const customer = await service.createCustomers(customersData)
|
||||
|
||||
expect(customer).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -245,7 +242,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("createCustomerGroup", () => {
|
||||
it("should create a single customer group", async () => {
|
||||
const group = await service.createCustomerGroup({
|
||||
const group = await service.createCustomerGroups({
|
||||
name: "VIP Customers",
|
||||
metadata: { priority: "high" },
|
||||
created_by: "admin",
|
||||
@@ -262,7 +259,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create multiple customer groups", async () => {
|
||||
const groups = await service.createCustomerGroup([
|
||||
const groups = await service.createCustomerGroups([
|
||||
{
|
||||
name: "VIP Customers",
|
||||
metadata: { priority: "high" },
|
||||
@@ -296,7 +293,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("list", () => {
|
||||
it("should list all customers when no filters are applied", async () => {
|
||||
await service.create([
|
||||
await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -309,7 +306,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const customers = await service.list()
|
||||
const customers = await service.listCustomers()
|
||||
|
||||
expect(customers.length).toBeGreaterThanOrEqual(2)
|
||||
expect(customers).toEqual(
|
||||
@@ -329,7 +326,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should list customers filtered by a specific email", async () => {
|
||||
await service.create([
|
||||
await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -343,7 +340,7 @@ moduleIntegrationTestRunner({
|
||||
])
|
||||
|
||||
const filter = { email: "unique.email@example.com" }
|
||||
const customers = await service.list(filter)
|
||||
const customers = await service.listCustomers(filter)
|
||||
|
||||
expect(customers.length).toBe(1)
|
||||
expect(customers[0]).toEqual(
|
||||
@@ -356,9 +353,9 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should list customers by a specific customer group", async () => {
|
||||
const vipGroup = await service.createCustomerGroup({ name: "VIP" })
|
||||
const vipGroup = await service.createCustomerGroups({ name: "VIP" })
|
||||
|
||||
const [john] = await service.create([
|
||||
const [john] = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -377,7 +374,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
const filter = { groups: vipGroup.id }
|
||||
const customers = await service.list(filter)
|
||||
const customers = await service.listCustomers(filter)
|
||||
|
||||
expect(customers).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -402,14 +399,14 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("addCustomerToGroup", () => {
|
||||
it("should add a single customer to a customer group", async () => {
|
||||
const [customer] = await service.create([
|
||||
const [customer] = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
email: "john.doe@example.com",
|
||||
},
|
||||
])
|
||||
const [group] = await service.createCustomerGroup([{ name: "VIP" }])
|
||||
const [group] = await service.createCustomerGroups([{ name: "VIP" }])
|
||||
|
||||
const result = await service.addCustomerToGroup({
|
||||
customer_id: customer.id,
|
||||
@@ -421,7 +418,7 @@ moduleIntegrationTestRunner({
|
||||
)
|
||||
|
||||
// Additional validation (optional): retrieve the customer and check if the group is assigned
|
||||
const updatedCustomer = await service.retrieve(customer.id, {
|
||||
const updatedCustomer = await service.retrieveCustomer(customer.id, {
|
||||
relations: ["groups"],
|
||||
})
|
||||
expect(updatedCustomer.groups).toContainEqual(
|
||||
@@ -430,7 +427,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple customers to customer groups", async () => {
|
||||
const customers = await service.create([
|
||||
const customers = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -442,7 +439,7 @@ moduleIntegrationTestRunner({
|
||||
email: "jane.smith@example.com",
|
||||
},
|
||||
])
|
||||
const groups = await service.createCustomerGroup([
|
||||
const groups = await service.createCustomerGroups([
|
||||
{ name: "VIP" },
|
||||
{ name: "Regular" },
|
||||
])
|
||||
@@ -462,9 +459,12 @@ moduleIntegrationTestRunner({
|
||||
)
|
||||
|
||||
for (const customer of customers) {
|
||||
const updatedCustomer = await service.retrieve(customer.id, {
|
||||
relations: ["groups"],
|
||||
})
|
||||
const updatedCustomer = await service.retrieveCustomer(
|
||||
customer.id,
|
||||
{
|
||||
relations: ["groups"],
|
||||
}
|
||||
)
|
||||
expect(updatedCustomer.groups).toContainEqual(
|
||||
expect.objectContaining({
|
||||
id: groups[customers.indexOf(customer) % groups.length].id,
|
||||
@@ -476,7 +476,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("update", () => {
|
||||
it("should update a single customer", async () => {
|
||||
const [customer] = await service.create([
|
||||
const [customer] = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -485,7 +485,10 @@ moduleIntegrationTestRunner({
|
||||
])
|
||||
|
||||
const updateData = { first_name: "Jonathan" }
|
||||
const updatedCustomer = await service.update(customer.id, updateData)
|
||||
const updatedCustomer = await service.updateCustomers(
|
||||
customer.id,
|
||||
updateData
|
||||
)
|
||||
|
||||
expect(updatedCustomer).toEqual(
|
||||
expect.objectContaining({ id: customer.id, first_name: "Jonathan" })
|
||||
@@ -493,7 +496,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update multiple customers by IDs", async () => {
|
||||
const customers = await service.create([
|
||||
const customers = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -508,7 +511,10 @@ moduleIntegrationTestRunner({
|
||||
|
||||
const updateData = { last_name: "Updated" }
|
||||
const customerIds = customers.map((customer) => customer.id)
|
||||
const updatedCustomers = await service.update(customerIds, updateData)
|
||||
const updatedCustomers = await service.updateCustomers(
|
||||
customerIds,
|
||||
updateData
|
||||
)
|
||||
|
||||
updatedCustomers.forEach((updatedCustomer) => {
|
||||
expect(updatedCustomer).toEqual(
|
||||
@@ -518,7 +524,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update customers using a selector", async () => {
|
||||
await service.create([
|
||||
await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -533,7 +539,10 @@ moduleIntegrationTestRunner({
|
||||
|
||||
const selector = { last_name: "Doe" }
|
||||
const updateData = { last_name: "Updated" }
|
||||
const updatedCustomers = await service.update(selector, updateData)
|
||||
const updatedCustomers = await service.updateCustomers(
|
||||
selector,
|
||||
updateData
|
||||
)
|
||||
|
||||
updatedCustomers.forEach((updatedCustomer) => {
|
||||
expect(updatedCustomer).toEqual(
|
||||
@@ -545,7 +554,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("delete", () => {
|
||||
it("should delete a single customer", async () => {
|
||||
const [customer] = await service.create([
|
||||
const [customer] = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -553,15 +562,15 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
await service.delete(customer.id)
|
||||
await service.deleteCustomers(customer.id)
|
||||
|
||||
await expect(service.retrieve(customer.id)).rejects.toThrow(
|
||||
await expect(service.retrieveCustomer(customer.id)).rejects.toThrow(
|
||||
`Customer with id: ${customer.id} was not found`
|
||||
)
|
||||
})
|
||||
|
||||
it("should delete multiple customers by IDs", async () => {
|
||||
const customers = await service.create([
|
||||
const customers = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -575,17 +584,17 @@ moduleIntegrationTestRunner({
|
||||
])
|
||||
|
||||
const customerIds = customers.map((customer) => customer.id)
|
||||
await service.delete(customerIds)
|
||||
await service.deleteCustomers(customerIds)
|
||||
|
||||
for (const customer of customers) {
|
||||
await expect(service.retrieve(customer.id)).rejects.toThrow(
|
||||
await expect(service.retrieveCustomer(customer.id)).rejects.toThrow(
|
||||
`Customer with id: ${customer.id} was not found`
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("should delete customers using a selector", async () => {
|
||||
await service.create([
|
||||
await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -599,19 +608,21 @@ moduleIntegrationTestRunner({
|
||||
])
|
||||
|
||||
const selector = { last_name: "Doe" }
|
||||
await service.delete(selector)
|
||||
await service.deleteCustomers(selector)
|
||||
|
||||
const remainingCustomers = await service.list({ last_name: "Doe" })
|
||||
const remainingCustomers = await service.listCustomers({
|
||||
last_name: "Doe",
|
||||
})
|
||||
expect(remainingCustomers.length).toBe(0)
|
||||
})
|
||||
|
||||
it("should cascade address relationship when deleting customer", async () => {
|
||||
// Creating a customer and an address
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
await service.addAddresses({
|
||||
await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -620,12 +631,15 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
// verify that the address was added
|
||||
const customerWithAddress = await service.retrieve(customer.id, {
|
||||
relations: ["addresses"],
|
||||
})
|
||||
const customerWithAddress = await service.retrieveCustomer(
|
||||
customer.id,
|
||||
{
|
||||
relations: ["addresses"],
|
||||
}
|
||||
)
|
||||
expect(customerWithAddress.addresses?.length).toBe(1)
|
||||
|
||||
await service.delete(customer.id)
|
||||
await service.deleteCustomers(customer.id)
|
||||
|
||||
const res = await service.listAddresses({
|
||||
customer_id: customer.id,
|
||||
@@ -635,11 +649,11 @@ moduleIntegrationTestRunner({
|
||||
|
||||
it("should cascade relationship when deleting customer", async () => {
|
||||
// Creating a customer and a group
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const group = await service.createCustomerGroup({ name: "VIP" })
|
||||
const group = await service.createCustomerGroups({ name: "VIP" })
|
||||
|
||||
// Adding the customer to the groups
|
||||
await service.addCustomerToGroup({
|
||||
@@ -647,7 +661,7 @@ moduleIntegrationTestRunner({
|
||||
customer_group_id: group.id,
|
||||
})
|
||||
|
||||
await service.delete(customer.id)
|
||||
await service.deleteCustomers(customer.id)
|
||||
|
||||
const res = await service.listCustomerGroupCustomers({
|
||||
customer_id: customer.id,
|
||||
@@ -659,7 +673,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("deleteCustomerGroup", () => {
|
||||
it("should delete a single customer group", async () => {
|
||||
const [group] = await service.createCustomerGroup([{ name: "VIP" }])
|
||||
const [group] = await service.createCustomerGroups([{ name: "VIP" }])
|
||||
await service.deleteCustomerGroups(group.id)
|
||||
|
||||
await expect(
|
||||
@@ -670,7 +684,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should delete multiple customer groups by IDs", async () => {
|
||||
const groups = await service.createCustomerGroup([
|
||||
const groups = await service.createCustomerGroups([
|
||||
{ name: "VIP" },
|
||||
{ name: "Regular" },
|
||||
])
|
||||
@@ -688,7 +702,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should delete customer groups using a selector", async () => {
|
||||
await service.createCustomerGroup([
|
||||
await service.createCustomerGroups([
|
||||
{ name: "VIP" },
|
||||
{ name: "Regular" },
|
||||
])
|
||||
@@ -704,11 +718,11 @@ moduleIntegrationTestRunner({
|
||||
|
||||
it("should cascade relationship when deleting customer group", async () => {
|
||||
// Creating a customer and a group
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const group = await service.createCustomerGroup({ name: "VIP" })
|
||||
const group = await service.createCustomerGroups({ name: "VIP" })
|
||||
|
||||
// Adding the customer to the groups
|
||||
await service.addCustomerToGroup({
|
||||
@@ -728,18 +742,18 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("addAddresses", () => {
|
||||
it("should add a single address to a customer", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const address = await service.addAddresses({
|
||||
const address = await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
postal_code: "10001",
|
||||
country_code: "US",
|
||||
})
|
||||
const [customerWithAddress] = await service.list(
|
||||
const [customerWithAddress] = await service.listCustomers(
|
||||
{ id: customer.id },
|
||||
{ relations: ["addresses"] }
|
||||
)
|
||||
@@ -750,11 +764,11 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should add multiple addresses to a customer", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const addresses = await service.addAddresses([
|
||||
const addresses = await service.createAddresses([
|
||||
{
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
@@ -770,7 +784,7 @@ moduleIntegrationTestRunner({
|
||||
country_code: "US",
|
||||
},
|
||||
])
|
||||
const [customerWithAddresses] = await service.list(
|
||||
const [customerWithAddresses] = await service.listCustomers(
|
||||
{ id: customer.id },
|
||||
{ relations: ["addresses"] }
|
||||
)
|
||||
@@ -784,11 +798,11 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should only be possible to add one default shipping address per customer", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
await service.addAddresses({
|
||||
await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -796,7 +810,7 @@ moduleIntegrationTestRunner({
|
||||
country_code: "US",
|
||||
is_default_shipping: true,
|
||||
})
|
||||
await service.addAddresses({
|
||||
await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -806,7 +820,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.addAddresses({
|
||||
service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -820,11 +834,11 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should only be possible to add one default billing address per customer", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
await service.addAddresses({
|
||||
await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -832,7 +846,7 @@ moduleIntegrationTestRunner({
|
||||
country_code: "US",
|
||||
is_default_billing: true,
|
||||
})
|
||||
await service.addAddresses({
|
||||
await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -842,7 +856,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.addAddresses({
|
||||
service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -858,11 +872,11 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("updateAddresses", () => {
|
||||
it("should update a single address", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const address = await service.addAddresses({
|
||||
const address = await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
address_1: "123 Main St",
|
||||
@@ -873,7 +887,7 @@ moduleIntegrationTestRunner({
|
||||
address_1: "456 Main St",
|
||||
})
|
||||
|
||||
const updatedCustomer = await service.retrieve(customer.id, {
|
||||
const updatedCustomer = await service.retrieveCustomer(customer.id, {
|
||||
select: ["id"],
|
||||
relations: ["addresses"],
|
||||
})
|
||||
@@ -888,16 +902,16 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update multiple addresses", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const address1 = await service.addAddresses({
|
||||
const address1 = await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
address_1: "123 Main St",
|
||||
})
|
||||
const address2 = await service.addAddresses({
|
||||
const address2 = await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Work",
|
||||
address_1: "456 Main St",
|
||||
@@ -910,7 +924,7 @@ moduleIntegrationTestRunner({
|
||||
}
|
||||
)
|
||||
|
||||
const updatedCustomer = await service.retrieve(customer.id, {
|
||||
const updatedCustomer = await service.retrieveCustomer(customer.id, {
|
||||
select: ["id"],
|
||||
relations: ["addresses"],
|
||||
})
|
||||
@@ -930,11 +944,11 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should update multiple addresses with ids", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const [address1, address2] = await service.addAddresses([
|
||||
const [address1, address2] = await service.createAddresses([
|
||||
{
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
@@ -951,7 +965,7 @@ moduleIntegrationTestRunner({
|
||||
address_name: "Under Construction",
|
||||
})
|
||||
|
||||
const updatedCustomer = await service.retrieve(customer.id, {
|
||||
const updatedCustomer = await service.retrieveCustomer(customer.id, {
|
||||
select: ["id"],
|
||||
relations: ["addresses"],
|
||||
})
|
||||
@@ -971,7 +985,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should fail when updating address to a default shipping address when one already exists", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
addresses: [
|
||||
@@ -982,7 +996,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
],
|
||||
})
|
||||
const address = await service.addAddresses({
|
||||
const address = await service.createAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Work",
|
||||
address_1: "456 Main St",
|
||||
@@ -998,11 +1012,11 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("listAddresses", () => {
|
||||
it("should list all addresses for a customer", async () => {
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const [address1, address2] = await service.addAddresses([
|
||||
const [address1, address2] = await service.createAddresses([
|
||||
{
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
@@ -1040,14 +1054,14 @@ moduleIntegrationTestRunner({
|
||||
describe("removeCustomerFromGroup", () => {
|
||||
it("should remove a single customer from a group", async () => {
|
||||
// Creating a customer and a group
|
||||
const [customer] = await service.create([
|
||||
const [customer] = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
email: "john.doe@example.com",
|
||||
},
|
||||
])
|
||||
const [group] = await service.createCustomerGroup([{ name: "VIP" }])
|
||||
const [group] = await service.createCustomerGroups([{ name: "VIP" }])
|
||||
|
||||
// Adding the customer to the group
|
||||
await service.addCustomerToGroup({
|
||||
@@ -1055,7 +1069,7 @@ moduleIntegrationTestRunner({
|
||||
customer_group_id: group.id,
|
||||
})
|
||||
|
||||
const [customerInGroup] = await service.list(
|
||||
const [customerInGroup] = await service.listCustomers(
|
||||
{ id: customer.id },
|
||||
{ relations: ["groups"] }
|
||||
)
|
||||
@@ -1069,7 +1083,7 @@ moduleIntegrationTestRunner({
|
||||
customer_group_id: group.id,
|
||||
})
|
||||
|
||||
const [updatedCustomer] = await service.list(
|
||||
const [updatedCustomer] = await service.listCustomers(
|
||||
{ id: customer.id },
|
||||
{ relations: ["groups"] }
|
||||
)
|
||||
@@ -1078,7 +1092,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
it("should remove multiple customers from groups", async () => {
|
||||
// Creating multiple customers and groups
|
||||
const customers = await service.create([
|
||||
const customers = await service.createCustomers([
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -1090,7 +1104,7 @@ moduleIntegrationTestRunner({
|
||||
email: "jane.smith@example.com",
|
||||
},
|
||||
])
|
||||
const groups = await service.createCustomerGroup([
|
||||
const groups = await service.createCustomerGroups([
|
||||
{ name: "VIP" },
|
||||
{ name: "Regular" },
|
||||
])
|
||||
@@ -1111,7 +1125,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
// Verification for each customer
|
||||
for (const pair of pairsToRemove) {
|
||||
const [updatedCustomer] = await service.list(
|
||||
const [updatedCustomer] = await service.listCustomers(
|
||||
{ id: pair.customer_id },
|
||||
{ relations: ["groups"] }
|
||||
)
|
||||
@@ -1124,15 +1138,15 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("softDelete", () => {
|
||||
it("should soft delete a single customer", async () => {
|
||||
const [customer] = await service.create([
|
||||
const [customer] = await service.createCustomers([
|
||||
{ first_name: "John", last_name: "Doe" },
|
||||
])
|
||||
await service.softDelete([customer.id])
|
||||
await service.softDeleteCustomers([customer.id])
|
||||
|
||||
const res = await service.list({ id: customer.id })
|
||||
const res = await service.listCustomers({ id: customer.id })
|
||||
expect(res.length).toBe(0)
|
||||
|
||||
const deletedCustomer = await service.retrieve(customer.id, {
|
||||
const deletedCustomer = await service.retrieveCustomer(customer.id, {
|
||||
withDeleted: true,
|
||||
})
|
||||
|
||||
@@ -1140,17 +1154,17 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should soft delete multiple customers", async () => {
|
||||
const customers = await service.create([
|
||||
const customers = await service.createCustomers([
|
||||
{ first_name: "John", last_name: "Doe" },
|
||||
{ first_name: "Jane", last_name: "Smith" },
|
||||
])
|
||||
const customerIds = customers.map((customer) => customer.id)
|
||||
await service.softDelete(customerIds)
|
||||
await service.softDeleteCustomers(customerIds)
|
||||
|
||||
const res = await service.list({ id: customerIds })
|
||||
const res = await service.listCustomers({ id: customerIds })
|
||||
expect(res.length).toBe(0)
|
||||
|
||||
const deletedCustomers = await service.list(
|
||||
const deletedCustomers = await service.listCustomers(
|
||||
{ id: customerIds },
|
||||
{ withDeleted: true }
|
||||
)
|
||||
@@ -1159,11 +1173,11 @@ moduleIntegrationTestRunner({
|
||||
|
||||
it("should remove customer in group relation", async () => {
|
||||
// Creating a customer and a group
|
||||
const customer = await service.create({
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const group = await service.createCustomerGroup({ name: "VIP" })
|
||||
const group = await service.createCustomerGroups({ name: "VIP" })
|
||||
|
||||
// Adding the customer to the group
|
||||
await service.addCustomerToGroup({
|
||||
@@ -1171,7 +1185,7 @@ moduleIntegrationTestRunner({
|
||||
customer_group_id: group.id,
|
||||
})
|
||||
|
||||
await service.softDelete([customer.id])
|
||||
await service.softDeleteCustomers([customer.id])
|
||||
|
||||
const resGroup = await service.retrieveCustomerGroup(group.id, {
|
||||
relations: ["customers"],
|
||||
@@ -1182,36 +1196,36 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("restore", () => {
|
||||
it("should restore a single customer", async () => {
|
||||
const [customer] = await service.create([
|
||||
const [customer] = await service.createCustomers([
|
||||
{ first_name: "John", last_name: "Doe" },
|
||||
])
|
||||
await service.softDelete([customer.id])
|
||||
await service.softDeleteCustomers([customer.id])
|
||||
|
||||
const res = await service.list({ id: customer.id })
|
||||
const res = await service.listCustomers({ id: customer.id })
|
||||
expect(res.length).toBe(0)
|
||||
|
||||
await service.restore([customer.id])
|
||||
await service.restoreCustomers([customer.id])
|
||||
|
||||
const restoredCustomer = await service.retrieve(customer.id, {
|
||||
const restoredCustomer = await service.retrieveCustomer(customer.id, {
|
||||
withDeleted: true,
|
||||
})
|
||||
expect(restoredCustomer.deleted_at).toBeNull()
|
||||
})
|
||||
|
||||
it("should restore multiple customers", async () => {
|
||||
const customers = await service.create([
|
||||
const customers = await service.createCustomers([
|
||||
{ first_name: "John", last_name: "Doe" },
|
||||
{ first_name: "Jane", last_name: "Smith" },
|
||||
])
|
||||
const customerIds = customers.map((customer) => customer.id)
|
||||
await service.softDelete(customerIds)
|
||||
await service.softDeleteCustomers(customerIds)
|
||||
|
||||
const res = await service.list({ id: customerIds })
|
||||
const res = await service.listCustomers({ id: customerIds })
|
||||
expect(res.length).toBe(0)
|
||||
|
||||
await service.restore(customerIds)
|
||||
await service.restoreCustomers(customerIds)
|
||||
|
||||
const restoredCustomers = await service.list(
|
||||
const restoredCustomers = await service.listCustomers(
|
||||
{ id: customerIds },
|
||||
{ withDeleted: true }
|
||||
)
|
||||
@@ -1221,7 +1235,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("softDeleteCustomerGroup", () => {
|
||||
it("should soft delete a single customer group", async () => {
|
||||
const [group] = await service.createCustomerGroup([{ name: "VIP" }])
|
||||
const [group] = await service.createCustomerGroups([{ name: "VIP" }])
|
||||
await service.softDeleteCustomerGroups([group.id])
|
||||
|
||||
const res = await service.listCustomerGroups({ id: group.id })
|
||||
@@ -1235,7 +1249,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should soft delete multiple customer groups", async () => {
|
||||
const groups = await service.createCustomerGroup([
|
||||
const groups = await service.createCustomerGroups([
|
||||
{ name: "VIP" },
|
||||
{ name: "Regular" },
|
||||
])
|
||||
@@ -1255,7 +1269,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("restoreCustomerGroup", () => {
|
||||
it("should restore a single customer group", async () => {
|
||||
const [group] = await service.createCustomerGroup([{ name: "VIP" }])
|
||||
const [group] = await service.createCustomerGroups([{ name: "VIP" }])
|
||||
await service.softDeleteCustomerGroups([group.id])
|
||||
|
||||
const res = await service.listCustomerGroups({ id: group.id })
|
||||
@@ -1270,7 +1284,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should restore multiple customer groups", async () => {
|
||||
const groups = await service.createCustomerGroup([
|
||||
const groups = await service.createCustomerGroups([
|
||||
{ name: "VIP" },
|
||||
{ name: "Regular" },
|
||||
])
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-customer-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { CustomerModuleService } from "@services"
|
||||
|
||||
const moduleDefinition: ModuleExports = {
|
||||
service: CustomerModuleService,
|
||||
}
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,49 +1,21 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import { Address, Customer, CustomerGroup } from "@models"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const LinkableKeys = {
|
||||
customer_id: Customer.name,
|
||||
customer_group_id: CustomerGroup.name,
|
||||
customer_address_id: Address.name,
|
||||
}
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.CUSTOMER,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
export const joinerConfig = defineJoinerConfig(Modules.CUSTOMER, {
|
||||
alias: [
|
||||
{
|
||||
name: ["customer", "customers"],
|
||||
args: {
|
||||
entity: Customer.name,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["customer_group", "customer_groups"],
|
||||
args: {
|
||||
entity: CustomerGroup.name,
|
||||
methodSuffix: "CustomerGroups",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["customer_address", "customer_addresses"],
|
||||
args: {
|
||||
entity: Address.name,
|
||||
entity: "Address",
|
||||
methodSuffix: "Addresses",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -2,3 +2,4 @@ export { default as Address } from "./address"
|
||||
export { default as Customer } from "./customer"
|
||||
export { default as CustomerGroup } from "./customer-group"
|
||||
export { default as CustomerGroupCustomer } from "./customer-group-customer"
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { CustomerModuleService } from "@services"
|
||||
|
||||
const service = CustomerModuleService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { EOL } from "os"
|
||||
import { run } from "../seed"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-cart-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -1,58 +0,0 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as CustomerModels from "@models"
|
||||
import { EOL } from "os"
|
||||
import { resolve } from "path"
|
||||
|
||||
export async function run({
|
||||
options,
|
||||
logger,
|
||||
path,
|
||||
}: Partial<
|
||||
Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
>
|
||||
> & {
|
||||
path: string
|
||||
}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
logger.info(`Loading seed data from ${path}...`)
|
||||
|
||||
const { customerData } = await import(resolve(process.cwd(), path)).catch(
|
||||
(e) => {
|
||||
logger?.error(
|
||||
`Failed to load seed data from ${path}. Please, provide a relative path and check that you export the following: customerData.${EOL}${e}`
|
||||
)
|
||||
throw e
|
||||
}
|
||||
)
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(Modules.CUSTOMER, options)!
|
||||
const entities = Object.values(CustomerModels) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
const manager = orm.em.fork()
|
||||
|
||||
try {
|
||||
logger.info("Seeding customer data..")
|
||||
|
||||
// TODO: implement customer seed data
|
||||
// await createCustomers(manager, customersData)
|
||||
} catch (e) {
|
||||
logger.error(
|
||||
`Failed to insert the seed data in the PostgreSQL database ${dbData.clientUrl}.${EOL}${e}`
|
||||
)
|
||||
}
|
||||
|
||||
await orm.close(true)
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
import {
|
||||
Context,
|
||||
CustomerAddressDTO,
|
||||
CustomerDTO,
|
||||
CustomerGroupCustomerDTO,
|
||||
CustomerGroupDTO,
|
||||
CustomerTypes,
|
||||
DAL,
|
||||
ICustomerModuleService,
|
||||
@@ -14,16 +17,16 @@ import {
|
||||
InjectTransactionManager,
|
||||
isString,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
MedusaService,
|
||||
} from "@medusajs/utils"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
import { EntityManager } from "@mikro-orm/core"
|
||||
import {
|
||||
Address,
|
||||
Customer,
|
||||
CustomerGroup,
|
||||
CustomerGroupCustomer,
|
||||
} from "@models"
|
||||
import { EntityManager } from "@mikro-orm/core"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
@@ -33,33 +36,23 @@ type InjectedDependencies = {
|
||||
customerGroupCustomerService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
const generateMethodForModels = {
|
||||
Address: { singular: "Address", plural: "Addresses" },
|
||||
CustomerGroup,
|
||||
CustomerGroupCustomer,
|
||||
}
|
||||
|
||||
export default class CustomerModuleService<
|
||||
TAddress extends Address = Address,
|
||||
TCustomer extends Customer = Customer,
|
||||
TCustomerGroup extends CustomerGroup = CustomerGroup,
|
||||
TCustomerGroupCustomer extends CustomerGroupCustomer = CustomerGroupCustomer
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
CustomerDTO,
|
||||
{
|
||||
Address: { dto: any }
|
||||
CustomerGroup: { dto: any }
|
||||
CustomerGroupCustomer: { dto: any }
|
||||
}
|
||||
>(Customer, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class CustomerModuleService
|
||||
extends MedusaService<{
|
||||
Address: { dto: CustomerAddressDTO }
|
||||
Customer: { dto: CustomerDTO }
|
||||
CustomerGroup: { dto: CustomerGroupDTO }
|
||||
CustomerGroupCustomer: { dto: CustomerGroupCustomerDTO }
|
||||
}>(
|
||||
{ Address, Customer, CustomerGroup, CustomerGroupCustomer },
|
||||
entityNameToLinkableKeysMap
|
||||
)
|
||||
implements ICustomerModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected customerService_: ModulesSdkTypes.IMedusaInternalService<TCustomer>
|
||||
protected addressService_: ModulesSdkTypes.IMedusaInternalService<TAddress>
|
||||
protected customerGroupService_: ModulesSdkTypes.IMedusaInternalService<TCustomerGroup>
|
||||
protected customerGroupCustomerService_: ModulesSdkTypes.IMedusaInternalService<TCustomerGroupCustomer>
|
||||
protected customerService_: ModulesSdkTypes.IMedusaInternalService<Customer>
|
||||
protected addressService_: ModulesSdkTypes.IMedusaInternalService<Address>
|
||||
protected customerGroupService_: ModulesSdkTypes.IMedusaInternalService<CustomerGroup>
|
||||
protected customerGroupCustomerService_: ModulesSdkTypes.IMedusaInternalService<CustomerGroupCustomer>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -85,24 +78,25 @@ export default class CustomerModuleService<
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
async create(
|
||||
// @ts-expect-error
|
||||
async createCustomers(
|
||||
data: CustomerTypes.CreateCustomerDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerDTO>
|
||||
|
||||
async create(
|
||||
async createCustomers(
|
||||
data: CustomerTypes.CreateCustomerDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async create(
|
||||
async createCustomers(
|
||||
dataOrArray:
|
||||
| CustomerTypes.CreateCustomerDTO
|
||||
| CustomerTypes.CreateCustomerDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CustomerTypes.CustomerDTO | CustomerTypes.CustomerDTO[]> {
|
||||
const customers = await this.create_(dataOrArray, sharedContext)
|
||||
const customers = await this.createCustomers_(dataOrArray, sharedContext)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
CustomerTypes.CustomerDTO[]
|
||||
@@ -114,7 +108,7 @@ export default class CustomerModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async create_(
|
||||
async createCustomers_(
|
||||
dataOrArray:
|
||||
| CustomerTypes.CreateCustomerDTO
|
||||
| CustomerTypes.CreateCustomerDTO[],
|
||||
@@ -137,29 +131,30 @@ export default class CustomerModuleService<
|
||||
})
|
||||
.flat()
|
||||
|
||||
await this.addAddresses(addressDataWithCustomerIds, sharedContext)
|
||||
await this.createAddresses(addressDataWithCustomerIds, sharedContext)
|
||||
|
||||
return customers as unknown as CustomerTypes.CustomerDTO[]
|
||||
}
|
||||
|
||||
update(
|
||||
// @ts-expect-error
|
||||
updateCustomers(
|
||||
customerId: string,
|
||||
data: CustomerTypes.CustomerUpdatableFields,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerDTO>
|
||||
update(
|
||||
updateCustomers(
|
||||
customerIds: string[],
|
||||
data: CustomerTypes.CustomerUpdatableFields,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerDTO[]>
|
||||
update(
|
||||
updateCustomers(
|
||||
selector: CustomerTypes.FilterableCustomerProps,
|
||||
data: CustomerTypes.CustomerUpdatableFields,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerDTO[]>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async update(
|
||||
async updateCustomers(
|
||||
idsOrSelector: string | string[] | CustomerTypes.FilterableCustomerProps,
|
||||
data: CustomerTypes.CustomerUpdatableFields,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -203,18 +198,19 @@ export default class CustomerModuleService<
|
||||
return isString(idsOrSelector) ? serialized[0] : serialized
|
||||
}
|
||||
|
||||
async createCustomerGroup(
|
||||
// @ts-expect-error
|
||||
async createCustomerGroups(
|
||||
dataOrArrayOfData: CustomerTypes.CreateCustomerGroupDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerGroupDTO>
|
||||
|
||||
async createCustomerGroup(
|
||||
async createCustomerGroups(
|
||||
dataOrArrayOfData: CustomerTypes.CreateCustomerGroupDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerGroupDTO[]>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createCustomerGroup(
|
||||
async createCustomerGroups(
|
||||
dataOrArrayOfData:
|
||||
| CustomerTypes.CreateCustomerGroupDTO
|
||||
| CustomerTypes.CreateCustomerGroupDTO[],
|
||||
@@ -232,7 +228,7 @@ export default class CustomerModuleService<
|
||||
})
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
// @ts-expect-error
|
||||
async updateCustomerGroups(
|
||||
groupId: string,
|
||||
data: CustomerTypes.CustomerGroupUpdatableFields,
|
||||
@@ -319,7 +315,7 @@ export default class CustomerModuleService<
|
||||
)
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return (groupCustomers as unknown as TCustomerGroupCustomer[]).map(
|
||||
return (groupCustomers as unknown as CustomerGroupCustomer[]).map(
|
||||
(gc) => ({ id: gc.id })
|
||||
)
|
||||
}
|
||||
@@ -327,18 +323,18 @@ export default class CustomerModuleService<
|
||||
return { id: groupCustomers.id }
|
||||
}
|
||||
|
||||
// TODO: should be createAddresses to conform to the convention
|
||||
async addAddresses(
|
||||
// @ts-expect-error
|
||||
async createAddresses(
|
||||
addresses: CustomerTypes.CreateCustomerAddressDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerAddressDTO[]>
|
||||
async addAddresses(
|
||||
async createAddresses(
|
||||
address: CustomerTypes.CreateCustomerAddressDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerAddressDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async addAddresses(
|
||||
async createAddresses(
|
||||
data:
|
||||
| CustomerTypes.CreateCustomerAddressDTO
|
||||
| CustomerTypes.CreateCustomerAddressDTO[],
|
||||
@@ -346,7 +342,7 @@ export default class CustomerModuleService<
|
||||
): Promise<
|
||||
CustomerTypes.CustomerAddressDTO | CustomerTypes.CustomerAddressDTO[]
|
||||
> {
|
||||
const addresses = await this.addAddresses_(data, sharedContext)
|
||||
const addresses = await this.createAddresses_(data, sharedContext)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
CustomerTypes.CustomerAddressDTO[]
|
||||
@@ -360,7 +356,7 @@ export default class CustomerModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
private async addAddresses_(
|
||||
private async createAddresses_(
|
||||
data:
|
||||
| CustomerTypes.CreateCustomerAddressDTO
|
||||
| CustomerTypes.CreateCustomerAddressDTO[],
|
||||
@@ -372,7 +368,7 @@ export default class CustomerModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
// @ts-expect-error
|
||||
async updateAddresses(
|
||||
addressId: string,
|
||||
data: CustomerTypes.UpdateCustomerAddressDTO,
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
"access": "public"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
"access": "public"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { resolve } from "path"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { Entity, PrimaryKey } from "@mikro-orm/core"
|
||||
import { IFileModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
@@ -28,15 +29,14 @@ const moduleOptions = {
|
||||
],
|
||||
}
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IFileModuleService>({
|
||||
moduleName: Modules.FILE,
|
||||
moduleOptions: moduleOptions,
|
||||
moduleModels: [DummyEntity],
|
||||
// TODO: Fix the type of service, it complains for some reason if we pass IFileModuleService
|
||||
testSuite: ({ service }: SuiteOptions<any>) => {
|
||||
testSuite: ({ service }) => {
|
||||
describe("File Module Service", () => {
|
||||
it("creates and gets a file", async () => {
|
||||
const res = await service.create({
|
||||
const res = await service.createFiles({
|
||||
filename: "test.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
content: Buffer.from("test"),
|
||||
@@ -48,7 +48,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
// The fake provider returns the file content as the url
|
||||
const downloadUrl = await service.retrieve("test.jpg")
|
||||
const downloadUrl = await service.retrieveFile("test.jpg")
|
||||
expect(await new Response(downloadUrl.url).text()).toEqual("test")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
export * from "./types"
|
||||
export * from "./services"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { FileModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
const loaders = [loadProviders] as any
|
||||
|
||||
const service = FileModuleService
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,22 +1,13 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const LinkableKeys = {}
|
||||
export const joinerConfig = defineJoinerConfig(Modules.FILE, {
|
||||
entityQueryingConfig: [{ name: "File" }],
|
||||
})
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.FILE,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["file", "files"],
|
||||
args: {
|
||||
entity: "File",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap({})
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { FileModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
const loaders = [loadProviders] as any
|
||||
|
||||
const service = FileModuleService
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
@@ -2,10 +2,10 @@ import {
|
||||
Context,
|
||||
CreateFileDTO,
|
||||
FileDTO,
|
||||
ModuleJoinerConfig,
|
||||
FileTypes,
|
||||
FilterableFileProps,
|
||||
FindConfig,
|
||||
ModuleJoinerConfig,
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
@@ -26,10 +26,13 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
create(data: CreateFileDTO[], sharedContext?: Context): Promise<FileDTO[]>
|
||||
create(data: CreateFileDTO, sharedContext?: Context): Promise<FileDTO>
|
||||
createFiles(
|
||||
data: CreateFileDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<FileDTO[]>
|
||||
createFiles(data: CreateFileDTO, sharedContext?: Context): Promise<FileDTO>
|
||||
|
||||
async create(
|
||||
async createFiles(
|
||||
data: CreateFileDTO[] | CreateFileDTO
|
||||
): Promise<FileDTO[] | FileDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
@@ -46,9 +49,9 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
|
||||
return Array.isArray(data) ? result : result[0]
|
||||
}
|
||||
|
||||
async delete(ids: string[], sharedContext?: Context): Promise<void>
|
||||
async delete(id: string, sharedContext?: Context): Promise<void>
|
||||
async delete(ids: string[] | string): Promise<void> {
|
||||
async deleteFiles(ids: string[], sharedContext?: Context): Promise<void>
|
||||
async deleteFiles(id: string, sharedContext?: Context): Promise<void>
|
||||
async deleteFiles(ids: string[] | string): Promise<void> {
|
||||
const input = Array.isArray(ids) ? ids : [ids]
|
||||
await Promise.all(
|
||||
input.map((id) => this.fileProviderService_.delete({ fileKey: id }))
|
||||
@@ -57,7 +60,7 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
|
||||
return
|
||||
}
|
||||
|
||||
async retrieve(id: string): Promise<FileDTO> {
|
||||
async retrieveFile(id: string): Promise<FileDTO> {
|
||||
const res = await this.fileProviderService_.getPresignedDownloadUrl({
|
||||
fileKey: id,
|
||||
})
|
||||
@@ -68,7 +71,7 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
|
||||
}
|
||||
}
|
||||
|
||||
async list(
|
||||
async listFiles(
|
||||
filters?: FilterableFileProps,
|
||||
config?: FindConfig<FileDTO>,
|
||||
sharedContext?: Context
|
||||
@@ -97,7 +100,7 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
|
||||
]
|
||||
}
|
||||
|
||||
async listAndCount(
|
||||
async listAndCountFiles(
|
||||
filters?: FilterableFileProps,
|
||||
config?: FindConfig<FileDTO>,
|
||||
sharedContext?: Context
|
||||
|
||||
@@ -21,7 +21,7 @@ export async function createFullDataStructure(
|
||||
name: "test_" + randomString,
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test_" + randomString,
|
||||
type: "test-type",
|
||||
})
|
||||
|
||||
+74
-45
@@ -1,4 +1,7 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { FulfillmentEvents, GeoZoneType, Modules } from "@medusajs/utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { MockEventBusService } from "medusa-test-utils/dist"
|
||||
import { buildExpectedEventMessageShape } from "../../__fixtures__"
|
||||
import {
|
||||
CreateFulfillmentSetDTO,
|
||||
CreateServiceZoneDTO,
|
||||
@@ -6,10 +9,6 @@ import {
|
||||
ServiceZoneDTO,
|
||||
UpdateFulfillmentSetDTO,
|
||||
} from "@medusajs/types"
|
||||
import { FulfillmentEvents, GeoZoneType } from "@medusajs/utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { MockEventBusService } from "medusa-test-utils/dist"
|
||||
import { buildExpectedEventMessageShape } from "../../__fixtures__"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
@@ -29,11 +28,11 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
describe("Fulfillment Module Service", () => {
|
||||
describe("read", () => {
|
||||
it("should list fulfillment sets with a filter", async function () {
|
||||
const createdSet1 = await service.create({
|
||||
const createdSet1 = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
const createdSet2 = await service.create({
|
||||
const createdSet2 = await service.createFulfillmentSets({
|
||||
name: "test2",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
@@ -67,7 +66,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
})
|
||||
|
||||
let listedSets = await service.list(
|
||||
let listedSets = await service.listFulfillmentSets(
|
||||
{
|
||||
type: createdSet1.type,
|
||||
},
|
||||
@@ -76,7 +75,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
}
|
||||
)
|
||||
|
||||
const listedSets2 = await service.list(
|
||||
const listedSets2 = await service.listFulfillmentSets(
|
||||
{
|
||||
type: createdSet1.type,
|
||||
},
|
||||
@@ -101,7 +100,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
|
||||
expect(listedSets2).toEqual(listedSets2)
|
||||
|
||||
listedSets = await service.list({
|
||||
listedSets = await service.listFulfillmentSets({
|
||||
name: createdSet2.name,
|
||||
})
|
||||
|
||||
@@ -116,7 +115,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
])
|
||||
)
|
||||
|
||||
listedSets = await service.list({
|
||||
listedSets = await service.listFulfillmentSets({
|
||||
service_zones: { name: "test" },
|
||||
})
|
||||
|
||||
@@ -131,7 +130,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
])
|
||||
)
|
||||
|
||||
listedSets = await service.list({
|
||||
listedSets = await service.listFulfillmentSets({
|
||||
service_zones: { geo_zones: { country_code: "fr" } },
|
||||
})
|
||||
|
||||
@@ -156,7 +155,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
type: "test-type",
|
||||
}
|
||||
|
||||
const fulfillmentSet = await service.create(data)
|
||||
const fulfillmentSet = await service.createFulfillmentSets(data)
|
||||
|
||||
expect(fulfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -188,7 +187,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const fulfillmentSets = await service.create(data)
|
||||
const fulfillmentSets = await service.createFulfillmentSets(data)
|
||||
|
||||
expect(fulfillmentSets).toHaveLength(2)
|
||||
|
||||
@@ -228,7 +227,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
const fulfillmentSet = await service.create(data)
|
||||
const fulfillmentSet = await service.createFulfillmentSets(data)
|
||||
|
||||
expect(fulfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -291,7 +290,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const fulfillmentSets = await service.create(data)
|
||||
const fulfillmentSets = await service.createFulfillmentSets(data)
|
||||
|
||||
expect(fulfillmentSets).toHaveLength(3)
|
||||
|
||||
@@ -349,7 +348,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
const fulfillmentSet = await service.create(data)
|
||||
const fulfillmentSet = await service.createFulfillmentSets(data)
|
||||
|
||||
expect(fulfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -445,7 +444,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const fulfillmentSets = await service.create(data)
|
||||
const fulfillmentSets = await service.createFulfillmentSets(data)
|
||||
|
||||
expect(fulfillmentSets).toHaveLength(3)
|
||||
|
||||
@@ -508,8 +507,10 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
type: "test-type",
|
||||
}
|
||||
|
||||
await service.create(data)
|
||||
const err = await service.create(data).catch((e) => e)
|
||||
await service.createFulfillmentSets(data)
|
||||
const err = await service
|
||||
.createFulfillmentSets(data)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(err).toBeDefined()
|
||||
expect(err.message).toContain("exists")
|
||||
@@ -532,7 +533,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
let err = await service.create(data).catch((e) => e)
|
||||
let err = await service.createFulfillmentSets(data).catch((e) => e)
|
||||
expect(err.message).toBe(
|
||||
"Missing required property province_code for geo zone type province"
|
||||
)
|
||||
@@ -554,7 +555,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
err = await service.create(data).catch((e) => e)
|
||||
err = await service.createFulfillmentSets(data).catch((e) => e)
|
||||
expect(err.message).toBe(
|
||||
"Missing required property city for geo zone type city"
|
||||
)
|
||||
@@ -575,7 +576,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
err = await service.create(data).catch((e) => e)
|
||||
err = await service.createFulfillmentSets(data).catch((e) => e)
|
||||
expect(err.message).toBe(
|
||||
"Missing required property country_code for geo zone type zip"
|
||||
)
|
||||
@@ -596,7 +597,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
err = await service.create(data).catch((e) => e)
|
||||
err = await service.createFulfillmentSets(data).catch((e) => e)
|
||||
expect(err.message).toBe(`Invalid geo zone type: unknown`)
|
||||
})
|
||||
})
|
||||
@@ -608,7 +609,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
type: "test-type",
|
||||
}
|
||||
|
||||
const createdFulfillmentSet = await service.create(createData)
|
||||
const createdFulfillmentSet = await service.createFulfillmentSets(
|
||||
createData
|
||||
)
|
||||
|
||||
const updateData = {
|
||||
id: createdFulfillmentSet.id,
|
||||
@@ -616,7 +619,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
type: "updated-test-type",
|
||||
}
|
||||
|
||||
const updatedFulfillmentSets = await service.update(updateData)
|
||||
const updatedFulfillmentSets = await service.updateFulfillmentSets(
|
||||
updateData
|
||||
)
|
||||
|
||||
expect(updatedFulfillmentSets).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -648,7 +653,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const createdFulfillmentSets = await service.create(createData)
|
||||
const createdFulfillmentSets = await service.createFulfillmentSets(
|
||||
createData
|
||||
)
|
||||
|
||||
const updateData = createdFulfillmentSets.map(
|
||||
(fulfillmentSet, index) => ({
|
||||
@@ -658,8 +665,10 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
)
|
||||
|
||||
const updatedFulfillmentSets = await service.update(updateData)
|
||||
const fullfillmentSets = await service.list({
|
||||
const updatedFulfillmentSets = await service.updateFulfillmentSets(
|
||||
updateData
|
||||
)
|
||||
const fullfillmentSets = await service.listFulfillmentSets({
|
||||
id: updateData.map((ud) => ud.id),
|
||||
})
|
||||
|
||||
@@ -669,7 +678,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
for (const data_ of updateData) {
|
||||
const currentFullfillmentSet = fullfillmentSets.find(
|
||||
(fs) => fs.id === data_.id
|
||||
)
|
||||
)!
|
||||
|
||||
expect(currentFullfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -709,7 +718,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
const createdFulfillmentSet = await service.create(createData)
|
||||
const createdFulfillmentSet = await service.createFulfillmentSets(
|
||||
createData
|
||||
)
|
||||
|
||||
const createServiceZoneData: CreateServiceZoneDTO = {
|
||||
fulfillment_set_id: createdFulfillmentSet.id,
|
||||
@@ -729,7 +740,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
service_zones: [createServiceZoneData],
|
||||
}
|
||||
|
||||
const updatedFulfillmentSet = await service.update(updateData)
|
||||
const updatedFulfillmentSet = await service.updateFulfillmentSets(
|
||||
updateData
|
||||
)
|
||||
|
||||
expect(updatedFulfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -822,7 +835,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
const createdFulfillmentSet = await service.create(createData)
|
||||
const createdFulfillmentSet = await service.createFulfillmentSets(
|
||||
createData
|
||||
)
|
||||
|
||||
const createServiceZoneData: CreateServiceZoneDTO = {
|
||||
fulfillment_set_id: createdFulfillmentSet.id,
|
||||
@@ -845,7 +860,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}
|
||||
|
||||
const updatedFulfillmentSet = await service.update(updateData)
|
||||
const updatedFulfillmentSet = await service.updateFulfillmentSets(
|
||||
updateData
|
||||
)
|
||||
|
||||
expect(updatedFulfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -876,7 +893,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
|
||||
const createdServiceZone = updatedFulfillmentSet.service_zones.find(
|
||||
(s) => s.name === "service-zone-test2"
|
||||
)
|
||||
)!
|
||||
|
||||
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(3)
|
||||
expect(eventBusEmitSpy).toHaveBeenLastCalledWith(
|
||||
@@ -917,7 +934,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const createdFulfillmentSets = await service.create(createData)
|
||||
const createdFulfillmentSets = await service.createFulfillmentSets(
|
||||
createData
|
||||
)
|
||||
|
||||
const updateData = {
|
||||
id: createdFulfillmentSets[1].id,
|
||||
@@ -925,7 +944,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
type: "updated-test-type2",
|
||||
}
|
||||
|
||||
const err = await service.update(updateData).catch((e) => e)
|
||||
const err = await service
|
||||
.updateFulfillmentSets(updateData)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(err).toBeDefined()
|
||||
expect(err.message).toContain("exists")
|
||||
@@ -965,7 +986,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const createdFulfillmentSets = await service.create(createData)
|
||||
const createdFulfillmentSets = await service.createFulfillmentSets(
|
||||
createData
|
||||
)
|
||||
|
||||
const updateData: UpdateFulfillmentSetDTO[] =
|
||||
createdFulfillmentSets.map((fulfillmentSet, index) => ({
|
||||
@@ -985,7 +1008,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}))
|
||||
|
||||
const updatedFulfillmentSets = await service.update(updateData)
|
||||
const updatedFulfillmentSets = await service.updateFulfillmentSets(
|
||||
updateData
|
||||
)
|
||||
|
||||
expect(updatedFulfillmentSets).toHaveLength(2)
|
||||
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(10)
|
||||
@@ -993,10 +1018,10 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
for (const data_ of updateData) {
|
||||
const expectedFulfillmentSet = updatedFulfillmentSets.find(
|
||||
(f) => f.id === data_.id
|
||||
)
|
||||
)!
|
||||
const originalFulfillmentSet = createdFulfillmentSets.find(
|
||||
(f) => f.id === data_.id
|
||||
)
|
||||
)!
|
||||
|
||||
expect(expectedFulfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -1115,7 +1140,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const createdFulfillmentSets = await service.create(createData)
|
||||
const createdFulfillmentSets = await service.createFulfillmentSets(
|
||||
createData
|
||||
)
|
||||
|
||||
const updateData: UpdateFulfillmentSetDTO[] =
|
||||
createdFulfillmentSets.map((fulfillmentSet, index) => ({
|
||||
@@ -1136,7 +1163,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
],
|
||||
}))
|
||||
|
||||
const updatedFulfillmentSets = await service.update(updateData)
|
||||
const updatedFulfillmentSets = await service.updateFulfillmentSets(
|
||||
updateData
|
||||
)
|
||||
|
||||
expect(updatedFulfillmentSets).toHaveLength(2)
|
||||
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(6)
|
||||
@@ -1144,7 +1173,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
for (const data_ of updateData) {
|
||||
const expectedFulfillmentSet = updatedFulfillmentSets.find(
|
||||
(f) => f.id === data_.id
|
||||
)
|
||||
)!
|
||||
expect(expectedFulfillmentSet).toEqual(
|
||||
expect.objectContaining({
|
||||
id: data_.id,
|
||||
@@ -1175,7 +1204,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
const createdServiceZone =
|
||||
expectedFulfillmentSet.service_zones.find((s) =>
|
||||
s.name.includes(`added-service-zone-test`)
|
||||
)
|
||||
)!
|
||||
|
||||
expect(eventBusEmitSpy).toHaveBeenLastCalledWith(
|
||||
expect.arrayContaining([
|
||||
|
||||
+5
-6
@@ -56,7 +56,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -104,7 +104,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -191,7 +191,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -280,7 +280,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -332,7 +332,6 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
)!
|
||||
|
||||
const updateData: UpdateFulfillmentDTO = {
|
||||
id: fulfillment.id,
|
||||
labels: [
|
||||
{ id: label1.id },
|
||||
{ ...label2, label_url: "updated-test-label-url-2" },
|
||||
@@ -417,7 +416,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
|
||||
+6
-6
@@ -29,7 +29,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
describe("Fulfillment Module Service", () => {
|
||||
describe("read", () => {
|
||||
it("should list geo zones with a filter", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -80,7 +80,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
describe("mutations", () => {
|
||||
describe("on create", () => {
|
||||
it("should create a new geo zone", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -119,7 +119,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should create a collection of geo zones", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -174,7 +174,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should fail to create new geo zones that are not valid", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -230,7 +230,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
|
||||
describe("on update", () => {
|
||||
it("should update an existing geo zone", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -277,7 +277,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should update a collection of geo zones", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
|
||||
+7
-7
@@ -30,7 +30,7 @@ let providerId = "fixtures-fulfillment-provider_test-provider"
|
||||
|
||||
async function list(
|
||||
service: IFulfillmentModuleService,
|
||||
...args: Parameters<IFulfillmentModuleService["list"]>
|
||||
...args: Parameters<IFulfillmentModuleService["listFulfillmentSets"]>
|
||||
) {
|
||||
const [filters = {}, config = {}] = args
|
||||
|
||||
@@ -48,7 +48,7 @@ async function list(
|
||||
...config,
|
||||
}
|
||||
|
||||
return await service.list(filters, finalConfig)
|
||||
return await service.listFulfillmentSets(filters, finalConfig)
|
||||
}
|
||||
|
||||
function expectSoftDeleted(
|
||||
@@ -109,7 +109,7 @@ moduleIntegrationTestRunner({
|
||||
it("should load and save all the providers on bootstrap with the correct is_enabled value", async () => {
|
||||
const databaseConfig = {
|
||||
schema: "public",
|
||||
clientUrl: MikroOrmWrapper.clientUrl,
|
||||
clientUrl: MikroOrmWrapper.clientUrl!,
|
||||
}
|
||||
|
||||
const providersConfig = {}
|
||||
@@ -159,7 +159,7 @@ moduleIntegrationTestRunner({
|
||||
name
|
||||
)
|
||||
)
|
||||
})
|
||||
})!
|
||||
expect(provider).toBeDefined()
|
||||
expect(provider.is_enabled).toBeTruthy()
|
||||
}
|
||||
@@ -222,7 +222,7 @@ moduleIntegrationTestRunner({
|
||||
name
|
||||
)
|
||||
)
|
||||
})
|
||||
})!
|
||||
expect(provider).toBeDefined()
|
||||
|
||||
const isEnabled = !!providersConfig2[name]
|
||||
@@ -242,7 +242,7 @@ moduleIntegrationTestRunner({
|
||||
* Soft delete the fulfillment set
|
||||
*/
|
||||
|
||||
await service.softDelete(fulfillmentSets[0].id)
|
||||
await service.softDeleteFulfillmentSets([fulfillmentSets[0].id])
|
||||
const deletedFulfillmentSets = await list(
|
||||
service,
|
||||
{},
|
||||
@@ -256,7 +256,7 @@ moduleIntegrationTestRunner({
|
||||
* Restore the fulfillment set
|
||||
*/
|
||||
|
||||
await service.restore(fulfillmentSets[0].id)
|
||||
await service.restoreFulfillmentSets([fulfillmentSets[0].id])
|
||||
const restoredFulfillmentSets = await list(service)
|
||||
expectSoftDeleted(restoredFulfillmentSets)
|
||||
})
|
||||
|
||||
+8
-8
@@ -30,7 +30,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
describe("Fulfillment Module Service", () => {
|
||||
describe("read", () => {
|
||||
it("should list service zones with a filter", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -85,7 +85,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
describe("mutations", () => {
|
||||
describe("on create", () => {
|
||||
it("should create a new service zone", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -119,7 +119,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should create a collection of service zones", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -181,7 +181,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should fail on duplicated service zone name", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -205,7 +205,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should fail on creating a service zone and new geo zones that are not valid", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -277,7 +277,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
|
||||
describe("on update", () => {
|
||||
it("should update an existing service zone", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -396,7 +396,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should fail on duplicated service zone name", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -449,7 +449,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
|
||||
describe("on upsert", () => {
|
||||
it("should upsert a collection of service zones", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
|
||||
+15
-15
@@ -57,7 +57,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
describe("Fulfillment Module Service", () => {
|
||||
describe("read", () => {
|
||||
it("should list shipping options with a filter", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
@@ -113,7 +113,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should list shipping options with a context", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
@@ -224,7 +224,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it(`should list the shipping options for a context with a specific address`, async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
@@ -341,7 +341,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should validate if a shipping option is applicable to a context", async function () {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
@@ -432,7 +432,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -509,7 +509,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -601,7 +601,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -637,7 +637,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
|
||||
describe("on update", () => {
|
||||
it("should update a shipping option", async () => {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -789,7 +789,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should update a shipping option without updating the rules or the type", async () => {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -880,7 +880,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should update a collection of shipping options", async () => {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -1027,7 +1027,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should fail to update a non-existent shipping option", async () => {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -1075,7 +1075,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should fail to update a shipping option when adding non existing rules", async () => {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -1120,7 +1120,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
})
|
||||
|
||||
it("should fail to update a shipping option when adding invalid rules", async () => {
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -1173,7 +1173,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
@@ -1250,7 +1250,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
||||
name: "test",
|
||||
type: "default",
|
||||
})
|
||||
const fulfillmentSet = await service.create({
|
||||
const fulfillmentSet = await service.createFulfillmentSets({
|
||||
name: "test",
|
||||
type: "test-type",
|
||||
})
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-fulfillment-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { FulfillmentModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
export * from "./types"
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
const service = FulfillmentModuleService
|
||||
const loaders = [loadProviders]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
export default moduleDefinition
|
||||
|
||||
// Module options types
|
||||
export { FulfillmentModuleOptions } from "./types"
|
||||
|
||||
@@ -1,94 +1,24 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
Fulfillment,
|
||||
FulfillmentProvider,
|
||||
FulfillmentSet,
|
||||
GeoZone,
|
||||
ServiceZone,
|
||||
ShippingOption,
|
||||
ShippingOptionRule,
|
||||
ShippingProfile,
|
||||
} from "@models"
|
||||
|
||||
export const LinkableKeys: Record<string, string> = {
|
||||
fulfillment_id: Fulfillment.name,
|
||||
fulfillment_set_id: FulfillmentSet.name,
|
||||
shipping_option_id: ShippingOption.name,
|
||||
shipping_option_rule_id: ShippingOptionRule.name,
|
||||
}
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
export const joinerConfig = defineJoinerConfig(Modules.FULFILLMENT, {
|
||||
linkableKeys: {
|
||||
fulfillment_id: Fulfillment.name,
|
||||
fulfillment_set_id: FulfillmentSet.name,
|
||||
shipping_option_id: ShippingOption.name,
|
||||
shipping_option_rule_id: ShippingOptionRule.name,
|
||||
},
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.FULFILLMENT,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["fulfillment_set", "fulfillment_sets"],
|
||||
args: {
|
||||
entity: FulfillmentSet.name,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["shipping_option", "shipping_options"],
|
||||
args: {
|
||||
entity: ShippingOption.name,
|
||||
methodSuffix: "ShippingOptions",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["shipping_profile", "shipping_profiles"],
|
||||
args: {
|
||||
entity: ShippingProfile.name,
|
||||
methodSuffix: "ShippingProfiles",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["fulfillment", "fulfillments"],
|
||||
args: {
|
||||
entity: Fulfillment.name,
|
||||
methodSuffix: "Fulfillments",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["fulfillment_provider", "fulfillment_providers"],
|
||||
args: {
|
||||
entity: FulfillmentProvider.name,
|
||||
methodSuffix: "FulfillmentProviders",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["service_zone", "service_zones"],
|
||||
args: {
|
||||
entity: ServiceZone.name,
|
||||
methodSuffix: "ServiceZones",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["geo_zone", "geo_zones"],
|
||||
args: {
|
||||
entity: GeoZone.name,
|
||||
methodSuffix: "GeoZones",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["shipping_option_rule", "shipping_option_rules"],
|
||||
args: {
|
||||
entity: ShippingOptionRule.name,
|
||||
methodSuffix: "ShippingOptionRules",
|
||||
},
|
||||
},
|
||||
],
|
||||
} as ModuleJoinerConfig
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { FulfillmentModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
const service = FulfillmentModuleService
|
||||
const loaders = [loadProviders]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import * as Models from "@models"
|
||||
import { EOL } from "os"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-fulfillment-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
const run = ModulesSdkUtils.buildSeedScript({
|
||||
moduleName: Modules.FULFILLMENT,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/../../migrations",
|
||||
seedHandler: async ({ manager, data }) => {
|
||||
// TODO: Add seed logic
|
||||
},
|
||||
})
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -53,6 +53,7 @@ import { buildCreatedShippingOptionEvents } from "../utils/events"
|
||||
import FulfillmentProviderService from "./fulfillment-provider"
|
||||
|
||||
const generateMethodForModels = {
|
||||
FulfillmentSet,
|
||||
ServiceZone,
|
||||
ShippingOption,
|
||||
GeoZone,
|
||||
@@ -76,41 +77,29 @@ type InjectedDependencies = {
|
||||
fulfillmentService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
export default class FulfillmentModuleService<
|
||||
TEntity extends FulfillmentSet = FulfillmentSet,
|
||||
TServiceZoneEntity extends ServiceZone = ServiceZone,
|
||||
TGeoZoneEntity extends GeoZone = GeoZone,
|
||||
TShippingProfileEntity extends ShippingProfile = ShippingProfile,
|
||||
TShippingOptionEntity extends ShippingOption = ShippingOption,
|
||||
TShippingOptionRuleEntity extends ShippingOptionRule = ShippingOptionRule,
|
||||
TSippingOptionTypeEntity extends ShippingOptionType = ShippingOptionType,
|
||||
TFulfillmentEntity extends Fulfillment = Fulfillment
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
FulfillmentTypes.FulfillmentSetDTO,
|
||||
{
|
||||
FulfillmentSet: { dto: FulfillmentTypes.FulfillmentSetDTO }
|
||||
ServiceZone: { dto: FulfillmentTypes.ServiceZoneDTO }
|
||||
ShippingOption: { dto: FulfillmentTypes.ShippingOptionDTO }
|
||||
GeoZone: { dto: FulfillmentTypes.GeoZoneDTO }
|
||||
ShippingProfile: { dto: FulfillmentTypes.ShippingProfileDTO }
|
||||
ShippingOptionRule: { dto: FulfillmentTypes.ShippingOptionRuleDTO }
|
||||
ShippingOptionType: { dto: FulfillmentTypes.ShippingOptionTypeDTO }
|
||||
FulfillmentProvider: { dto: FulfillmentTypes.FulfillmentProviderDTO }
|
||||
}
|
||||
>(FulfillmentSet, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class FulfillmentModuleService
|
||||
extends ModulesSdkUtils.MedusaService<{
|
||||
FulfillmentSet: { dto: FulfillmentTypes.FulfillmentSetDTO }
|
||||
ServiceZone: { dto: FulfillmentTypes.ServiceZoneDTO }
|
||||
ShippingOption: { dto: FulfillmentTypes.ShippingOptionDTO }
|
||||
GeoZone: { dto: FulfillmentTypes.GeoZoneDTO }
|
||||
ShippingProfile: { dto: FulfillmentTypes.ShippingProfileDTO }
|
||||
ShippingOptionRule: { dto: FulfillmentTypes.ShippingOptionRuleDTO }
|
||||
ShippingOptionType: { dto: FulfillmentTypes.ShippingOptionTypeDTO }
|
||||
FulfillmentProvider: { dto: FulfillmentTypes.FulfillmentProviderDTO }
|
||||
}>(generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements IFulfillmentModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly fulfillmentSetService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
|
||||
protected readonly serviceZoneService_: ModulesSdkTypes.IMedusaInternalService<TServiceZoneEntity>
|
||||
protected readonly geoZoneService_: ModulesSdkTypes.IMedusaInternalService<TGeoZoneEntity>
|
||||
protected readonly shippingProfileService_: ModulesSdkTypes.IMedusaInternalService<TShippingProfileEntity>
|
||||
protected readonly shippingOptionService_: ModulesSdkTypes.IMedusaInternalService<TShippingOptionEntity>
|
||||
protected readonly shippingOptionRuleService_: ModulesSdkTypes.IMedusaInternalService<TShippingOptionRuleEntity>
|
||||
protected readonly shippingOptionTypeService_: ModulesSdkTypes.IMedusaInternalService<TSippingOptionTypeEntity>
|
||||
protected readonly fulfillmentSetService_: ModulesSdkTypes.IMedusaInternalService<FulfillmentSet>
|
||||
protected readonly serviceZoneService_: ModulesSdkTypes.IMedusaInternalService<ServiceZone>
|
||||
protected readonly geoZoneService_: ModulesSdkTypes.IMedusaInternalService<GeoZone>
|
||||
protected readonly shippingProfileService_: ModulesSdkTypes.IMedusaInternalService<ShippingProfile>
|
||||
protected readonly shippingOptionService_: ModulesSdkTypes.IMedusaInternalService<ShippingOption>
|
||||
protected readonly shippingOptionRuleService_: ModulesSdkTypes.IMedusaInternalService<ShippingOptionRule>
|
||||
protected readonly shippingOptionTypeService_: ModulesSdkTypes.IMedusaInternalService<ShippingOptionType>
|
||||
protected readonly fulfillmentProviderService_: FulfillmentProviderService
|
||||
protected readonly fulfillmentService_: ModulesSdkTypes.IMedusaInternalService<TFulfillmentEntity>
|
||||
protected readonly fulfillmentService_: ModulesSdkTypes.IMedusaInternalService<Fulfillment>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -258,18 +247,19 @@ export default class FulfillmentModuleService<
|
||||
]
|
||||
}
|
||||
|
||||
create(
|
||||
// @ts-expect-error
|
||||
createFulfillmentSets(
|
||||
data: FulfillmentTypes.CreateFulfillmentSetDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<FulfillmentTypes.FulfillmentSetDTO[]>
|
||||
create(
|
||||
createFulfillmentSets(
|
||||
data: FulfillmentTypes.CreateFulfillmentSetDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<FulfillmentTypes.FulfillmentSetDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async create(
|
||||
async createFulfillmentSets(
|
||||
data:
|
||||
| FulfillmentTypes.CreateFulfillmentSetDTO
|
||||
| FulfillmentTypes.CreateFulfillmentSetDTO[],
|
||||
@@ -277,7 +267,10 @@ export default class FulfillmentModuleService<
|
||||
): Promise<
|
||||
FulfillmentTypes.FulfillmentSetDTO | FulfillmentTypes.FulfillmentSetDTO[]
|
||||
> {
|
||||
const createdFulfillmentSets = await this.create_(data, sharedContext)
|
||||
const createdFulfillmentSets = await this.createFulfillmentSets_(
|
||||
data,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const returnedFulfillmentSets = Array.isArray(data)
|
||||
? createdFulfillmentSets
|
||||
@@ -289,12 +282,12 @@ export default class FulfillmentModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async create_(
|
||||
protected async createFulfillmentSets_(
|
||||
data:
|
||||
| FulfillmentTypes.CreateFulfillmentSetDTO
|
||||
| FulfillmentTypes.CreateFulfillmentSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<FulfillmentSet[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -360,7 +353,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.CreateServiceZoneDTO[]
|
||||
| FulfillmentTypes.CreateServiceZoneDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TServiceZoneEntity[]> {
|
||||
): Promise<ServiceZone[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -424,7 +417,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.CreateShippingOptionDTO[]
|
||||
| FulfillmentTypes.CreateShippingOptionDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TShippingOptionEntity[]> {
|
||||
): Promise<ShippingOption[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -493,7 +486,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.CreateShippingProfileDTO[]
|
||||
| FulfillmentTypes.CreateShippingProfileDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TShippingProfileEntity[]> {
|
||||
): Promise<ShippingProfile[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -582,7 +575,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.CreateShippingOptionRuleDTO[]
|
||||
| FulfillmentTypes.CreateShippingOptionRuleDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TShippingOptionRuleEntity[]> {
|
||||
): Promise<ShippingOptionRule[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -698,24 +691,28 @@ export default class FulfillmentModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
update(
|
||||
// @ts-expect-error
|
||||
updateFulfillmentSets(
|
||||
data: FulfillmentTypes.UpdateFulfillmentSetDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<FulfillmentTypes.FulfillmentSetDTO[]>
|
||||
update(
|
||||
updateFulfillmentSets(
|
||||
data: FulfillmentTypes.UpdateFulfillmentSetDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<FulfillmentTypes.FulfillmentSetDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async update(
|
||||
async updateFulfillmentSets(
|
||||
data: UpdateFulfillmentSetDTO[] | UpdateFulfillmentSetDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<
|
||||
FulfillmentTypes.FulfillmentSetDTO[] | FulfillmentTypes.FulfillmentSetDTO
|
||||
> {
|
||||
const updatedFulfillmentSets = await this.update_(data, sharedContext)
|
||||
const updatedFulfillmentSets = await this.updateFulfillmentSets_(
|
||||
data,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
FulfillmentTypes.FulfillmentSetDTO | FulfillmentTypes.FulfillmentSetDTO[]
|
||||
@@ -723,10 +720,10 @@ export default class FulfillmentModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
protected async updateFulfillmentSets_(
|
||||
data: UpdateFulfillmentSetDTO[] | UpdateFulfillmentSetDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[] | TEntity> {
|
||||
): Promise<FulfillmentSet[] | FulfillmentSet> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -765,7 +762,7 @@ export default class FulfillmentModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
const fulfillmentSetMap = new Map<string, TEntity>(
|
||||
const fulfillmentSetMap = new Map<string, FulfillmentSet>(
|
||||
fulfillmentSets.map((f) => [f.id, f])
|
||||
)
|
||||
|
||||
@@ -989,7 +986,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.UpdateServiceZoneDTO[]
|
||||
| FulfillmentTypes.UpdateServiceZoneDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TServiceZoneEntity | TServiceZoneEntity[]> {
|
||||
): Promise<ServiceZone | ServiceZone[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -1028,7 +1025,7 @@ export default class FulfillmentModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
const serviceZoneMap = new Map<string, TServiceZoneEntity>(
|
||||
const serviceZoneMap = new Map<string, ServiceZone>(
|
||||
serviceZones.map((s) => [s.id, s])
|
||||
)
|
||||
|
||||
@@ -1185,7 +1182,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.UpsertServiceZoneDTO[]
|
||||
| FulfillmentTypes.UpsertServiceZoneDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TServiceZoneEntity[] | TServiceZoneEntity> {
|
||||
): Promise<ServiceZone[] | ServiceZone> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const forUpdate = input.filter(
|
||||
(serviceZone): serviceZone is FulfillmentTypes.UpdateServiceZoneDTO =>
|
||||
@@ -1196,8 +1193,8 @@ export default class FulfillmentModuleService<
|
||||
!serviceZone.id
|
||||
)
|
||||
|
||||
const created: TServiceZoneEntity[] = []
|
||||
const updated: TServiceZoneEntity[] = []
|
||||
const created: ServiceZone[] = []
|
||||
const updated: ServiceZone[] = []
|
||||
|
||||
if (forCreate.length) {
|
||||
const createdServiceZones = await this.createServiceZones_(
|
||||
@@ -1280,7 +1277,7 @@ export default class FulfillmentModuleService<
|
||||
async updateShippingOptions_(
|
||||
data: UpdateShippingOptionsInput[] | UpdateShippingOptionsInput,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TShippingOptionEntity | TShippingOptionEntity[]> {
|
||||
): Promise<ShippingOption | ShippingOption[]> {
|
||||
const dataArray = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!dataArray.length) {
|
||||
@@ -1506,7 +1503,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.UpsertShippingOptionDTO[]
|
||||
| FulfillmentTypes.UpsertShippingOptionDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TShippingOptionEntity[] | TShippingOptionEntity> {
|
||||
): Promise<ShippingOption[] | ShippingOption> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const forUpdate = input.filter(
|
||||
(shippingOption): shippingOption is UpdateShippingOptionsInput =>
|
||||
@@ -1519,8 +1516,8 @@ export default class FulfillmentModuleService<
|
||||
!shippingOption.id
|
||||
)
|
||||
|
||||
let created: TShippingOptionEntity[] = []
|
||||
let updated: TShippingOptionEntity[] = []
|
||||
let created: ShippingOption[] = []
|
||||
let updated: ShippingOption[] = []
|
||||
|
||||
if (forCreate.length) {
|
||||
const createdShippingOptions = await this.createShippingOptions_(
|
||||
@@ -1731,7 +1728,7 @@ export default class FulfillmentModuleService<
|
||||
| FulfillmentTypes.UpdateShippingOptionRuleDTO[]
|
||||
| FulfillmentTypes.UpdateShippingOptionRuleDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TShippingOptionRuleEntity | TShippingOptionRuleEntity[]> {
|
||||
): Promise<ShippingOptionRule | ShippingOptionRule[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
if (!data_.length) {
|
||||
@@ -1772,8 +1769,8 @@ export default class FulfillmentModuleService<
|
||||
id: string,
|
||||
data: FulfillmentTypes.UpdateFulfillmentDTO,
|
||||
@MedusaContext() sharedContext: Context
|
||||
): Promise<TFulfillmentEntity> {
|
||||
const existingFulfillment: TFulfillmentEntity =
|
||||
): Promise<Fulfillment> {
|
||||
const existingFulfillment: Fulfillment =
|
||||
await this.fulfillmentService_.retrieve(
|
||||
id,
|
||||
{
|
||||
|
||||
+29
-33
@@ -1,22 +1,18 @@
|
||||
import { IInventoryServiceNext, InventoryItemDTO } from "@medusajs/types"
|
||||
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { IInventoryService, InventoryItemDTO } from "@medusajs/types"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IInventoryService>({
|
||||
moduleName: Modules.INVENTORY,
|
||||
resolve: "@medusajs/inventory-next",
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IInventoryServiceNext>) => {
|
||||
testSuite: ({ service }) => {
|
||||
describe("Inventory Module Service", () => {
|
||||
describe("create", () => {
|
||||
it("should create an inventory item", async () => {
|
||||
const data = { sku: "test-sku", origin_country: "test-country" }
|
||||
const inventoryItem = await service.create(data)
|
||||
const inventoryItem = await service.createInventoryItems(data)
|
||||
|
||||
expect(inventoryItem).toEqual(
|
||||
expect.objectContaining({ id: expect.any(String), ...data })
|
||||
@@ -28,7 +24,7 @@ moduleIntegrationTestRunner({
|
||||
{ sku: "test-sku", origin_country: "test-country" },
|
||||
{ sku: "test-sku-1", origin_country: "test-country-1" },
|
||||
]
|
||||
const inventoryItems = await service.create(data)
|
||||
const inventoryItems = await service.createInventoryItems(data)
|
||||
|
||||
expect(inventoryItems).toEqual([
|
||||
expect.objectContaining({ id: expect.any(String), ...data[0] }),
|
||||
@@ -40,7 +36,7 @@ moduleIntegrationTestRunner({
|
||||
describe("createReservationItem", () => {
|
||||
let inventoryItem: InventoryItemDTO
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -169,7 +165,7 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -234,7 +230,7 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -245,13 +241,13 @@ moduleIntegrationTestRunner({
|
||||
id: inventoryItem.id,
|
||||
sku: "updated-sku",
|
||||
}
|
||||
const updated = await service.update(update)
|
||||
const updated = await service.updateInventoryItems(update)
|
||||
|
||||
expect(updated).toEqual(expect.objectContaining(update))
|
||||
})
|
||||
|
||||
it("should update multiple inventory items", async () => {
|
||||
const item2 = await service.create({
|
||||
const item2 = await service.createInventoryItems({
|
||||
sku: "test-sku-1",
|
||||
})
|
||||
|
||||
@@ -265,7 +261,7 @@ moduleIntegrationTestRunner({
|
||||
sku: "updated-sku-2",
|
||||
},
|
||||
]
|
||||
const updated = await service.update(updates)
|
||||
const updated = await service.updateInventoryItems(updates)
|
||||
|
||||
expect(updated).toEqual([
|
||||
expect.objectContaining(updates[0]),
|
||||
@@ -279,7 +275,7 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -323,7 +319,7 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -413,7 +409,7 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -526,7 +522,7 @@ moduleIntegrationTestRunner({
|
||||
describe("deleteReservationItemByLocationId", () => {
|
||||
let inventoryItem: InventoryItemDTO
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -619,7 +615,7 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -681,7 +677,7 @@ moduleIntegrationTestRunner({
|
||||
describe("deleteInventoryLevel", () => {
|
||||
let inventoryItem: InventoryItemDTO
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -720,7 +716,7 @@ moduleIntegrationTestRunner({
|
||||
describe("adjustInventory", () => {
|
||||
let inventoryItem: InventoryItemDTO
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -756,11 +752,11 @@ moduleIntegrationTestRunner({
|
||||
describe("retrieveInventoryLevelByItemAndLocation", () => {
|
||||
let inventoryItem: InventoryItemDTO
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
const inventoryItem1 = await service.create({
|
||||
const inventoryItem1 = await service.createInventoryItems({
|
||||
sku: "test-sku-1",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -797,11 +793,11 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
const inventoryItem1 = await service.create({
|
||||
const inventoryItem1 = await service.createInventoryItems({
|
||||
sku: "test-sku-1",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -851,12 +847,12 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
|
||||
const inventoryItem1 = await service.create({
|
||||
const inventoryItem1 = await service.createInventoryItems({
|
||||
sku: "test-sku-1",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -899,12 +895,12 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
|
||||
const inventoryItem1 = await service.create({
|
||||
const inventoryItem1 = await service.createInventoryItems({
|
||||
sku: "test-sku-1",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
@@ -965,7 +961,7 @@ moduleIntegrationTestRunner({
|
||||
let inventoryItem: InventoryItemDTO
|
||||
|
||||
beforeEach(async () => {
|
||||
inventoryItem = await service.create({
|
||||
inventoryItem = await service.createInventoryItems({
|
||||
sku: "test-sku",
|
||||
origin_country: "test-country",
|
||||
})
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import InventoryService from "./services/inventory-module"
|
||||
|
||||
const moduleDefinition: ModuleExports = {
|
||||
service: InventoryService,
|
||||
}
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,40 +1,17 @@
|
||||
import { InventoryItem, InventoryLevel, ReservationItem } from "./models"
|
||||
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import moduleSchema from "./schema"
|
||||
|
||||
export const LinkableKeys = {
|
||||
inventory_item_id: InventoryItem.name,
|
||||
inventory_level_id: InventoryLevel.name,
|
||||
reservation_item_id: ReservationItem.name,
|
||||
}
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.INVENTORY,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: {
|
||||
inventory_item_id: InventoryItem.name,
|
||||
inventory_level_id: InventoryLevel.name,
|
||||
reservation_item_id: ReservationItem.name,
|
||||
},
|
||||
schema: moduleSchema,
|
||||
export const joinerConfig = defineJoinerConfig(Modules.INVENTORY, {
|
||||
alias: [
|
||||
{
|
||||
name: ["inventory_items", "inventory_item", "inventory"],
|
||||
args: {
|
||||
entity: "InventoryItem",
|
||||
methodSuffix: "InventoryItems",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -57,4 +34,7 @@ export const joinerConfig: ModuleJoinerConfig = {
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import InventoryService from "./services/inventory-module"
|
||||
|
||||
const service = InventoryService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
@@ -2,8 +2,6 @@ import { InternalModuleDeclaration } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
Context,
|
||||
DAL,
|
||||
IInventoryServiceNext,
|
||||
InventoryNext,
|
||||
InventoryTypes,
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
@@ -20,13 +18,14 @@ import {
|
||||
isString,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
MedusaService,
|
||||
partitionArray,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { InventoryItem, InventoryLevel, ReservationItem } from "@models"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
import InventoryLevelService from "./inventory-level"
|
||||
import { IInventoryService } from "@medusajs/types/dist/inventory"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
@@ -35,38 +34,32 @@ type InjectedDependencies = {
|
||||
reservationItemService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
const generateMethodForModels = {
|
||||
InventoryItem,
|
||||
InventoryLevel,
|
||||
ReservationItem,
|
||||
}
|
||||
|
||||
export default class InventoryModuleService<
|
||||
TInventoryItem extends InventoryItem = InventoryItem,
|
||||
TInventoryLevel extends InventoryLevel = InventoryLevel,
|
||||
TReservationItem extends ReservationItem = ReservationItem
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
InventoryNext.InventoryItemDTO,
|
||||
{
|
||||
InventoryItem: {
|
||||
dto: InventoryNext.InventoryItemDTO
|
||||
}
|
||||
InventoryLevel: {
|
||||
dto: InventoryNext.InventoryLevelDTO
|
||||
}
|
||||
ReservationItem: {
|
||||
dto: InventoryNext.ReservationItemDTO
|
||||
}
|
||||
export default class InventoryModuleService
|
||||
extends MedusaService<{
|
||||
InventoryItem: {
|
||||
dto: InventoryTypes.InventoryItemDTO
|
||||
}
|
||||
>(InventoryItem, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements IInventoryServiceNext
|
||||
InventoryLevel: {
|
||||
dto: InventoryTypes.InventoryLevelDTO
|
||||
}
|
||||
ReservationItem: {
|
||||
dto: InventoryTypes.ReservationItemDTO
|
||||
}
|
||||
}>(
|
||||
{
|
||||
InventoryItem,
|
||||
InventoryLevel,
|
||||
ReservationItem,
|
||||
},
|
||||
entityNameToLinkableKeysMap
|
||||
)
|
||||
implements IInventoryService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
|
||||
protected readonly inventoryItemService_: ModulesSdkTypes.IMedusaInternalService<TInventoryItem>
|
||||
protected readonly reservationItemService_: ModulesSdkTypes.IMedusaInternalService<TReservationItem>
|
||||
protected readonly inventoryLevelService_: InventoryLevelService<TInventoryLevel>
|
||||
protected readonly inventoryItemService_: ModulesSdkTypes.IMedusaInternalService<InventoryItem>
|
||||
protected readonly reservationItemService_: ModulesSdkTypes.IMedusaInternalService<ReservationItem>
|
||||
protected readonly inventoryLevelService_: InventoryLevelService<InventoryLevel>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -97,7 +90,7 @@ export default class InventoryModuleService<
|
||||
| { id: string }
|
||||
)[],
|
||||
context: Context
|
||||
): Promise<InventoryNext.InventoryLevelDTO[]> {
|
||||
): Promise<InventoryTypes.InventoryLevelDTO[]> {
|
||||
const [idData, itemLocationData] = partitionArray(
|
||||
data,
|
||||
({ id }) => !!id
|
||||
@@ -117,12 +110,12 @@ export default class InventoryModuleService<
|
||||
context
|
||||
)
|
||||
|
||||
const inventoryLevelIdMap: Map<string, InventoryNext.InventoryLevelDTO> =
|
||||
const inventoryLevelIdMap: Map<string, InventoryTypes.InventoryLevelDTO> =
|
||||
new Map(inventoryLevels.map((level) => [level.id, level]))
|
||||
|
||||
const inventoryLevelItemLocationMap: Map<
|
||||
string,
|
||||
Map<string, InventoryNext.InventoryLevelDTO>
|
||||
Map<string, InventoryTypes.InventoryLevelDTO>
|
||||
> = inventoryLevels.reduce((acc, curr) => {
|
||||
const inventoryLevelMap = acc.get(curr.inventory_item_id) ?? new Map()
|
||||
inventoryLevelMap.set(curr.location_id, curr)
|
||||
@@ -214,23 +207,23 @@ export default class InventoryModuleService<
|
||||
|
||||
// @ts-ignore
|
||||
async createReservationItems(
|
||||
input: InventoryNext.CreateReservationItemInput[],
|
||||
input: InventoryTypes.CreateReservationItemInput[],
|
||||
context?: Context
|
||||
): Promise<InventoryNext.ReservationItemDTO[]>
|
||||
): Promise<InventoryTypes.ReservationItemDTO[]>
|
||||
async createReservationItems(
|
||||
input: InventoryNext.CreateReservationItemInput,
|
||||
input: InventoryTypes.CreateReservationItemInput,
|
||||
context?: Context
|
||||
): Promise<InventoryNext.ReservationItemDTO>
|
||||
): Promise<InventoryTypes.ReservationItemDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async createReservationItems(
|
||||
input:
|
||||
| InventoryNext.CreateReservationItemInput[]
|
||||
| InventoryNext.CreateReservationItemInput,
|
||||
| InventoryTypes.CreateReservationItemInput[]
|
||||
| InventoryTypes.CreateReservationItemInput,
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
InventoryNext.ReservationItemDTO[] | InventoryNext.ReservationItemDTO
|
||||
InventoryTypes.ReservationItemDTO[] | InventoryTypes.ReservationItemDTO
|
||||
> {
|
||||
const toCreate = Array.isArray(input) ? input : [input]
|
||||
const sanitized = toCreate.map((d) => ({
|
||||
@@ -254,7 +247,7 @@ export default class InventoryModuleService<
|
||||
)
|
||||
|
||||
const serializedReservations = await this.baseRepository_.serialize<
|
||||
InventoryNext.ReservationItemDTO[] | InventoryNext.ReservationItemDTO
|
||||
InventoryTypes.ReservationItemDTO[] | InventoryTypes.ReservationItemDTO
|
||||
>(created, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -266,9 +259,9 @@ export default class InventoryModuleService<
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createReservationItems_(
|
||||
input: InventoryNext.CreateReservationItemInput[],
|
||||
input: InventoryTypes.CreateReservationItemInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<TReservationItem[]> {
|
||||
): Promise<ReservationItem[]> {
|
||||
const inventoryLevels = await this.ensureInventoryLevels(
|
||||
input.map(({ location_id, inventory_item_id }) => ({
|
||||
location_id,
|
||||
@@ -311,30 +304,25 @@ export default class InventoryModuleService<
|
||||
return created
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an inventory item
|
||||
* @param input - the input object
|
||||
* @param context
|
||||
* @return The created inventory item
|
||||
*/
|
||||
create(
|
||||
input: InventoryNext.CreateInventoryItemInput,
|
||||
// @ts-expect-error
|
||||
createInventoryItems(
|
||||
input: InventoryTypes.CreateInventoryItemInput,
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryItemDTO>
|
||||
create(
|
||||
input: InventoryNext.CreateInventoryItemInput[],
|
||||
): Promise<InventoryTypes.InventoryItemDTO>
|
||||
createInventoryItems(
|
||||
input: InventoryTypes.CreateInventoryItemInput[],
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryItemDTO[]>
|
||||
): Promise<InventoryTypes.InventoryItemDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async create(
|
||||
async createInventoryItems(
|
||||
input:
|
||||
| InventoryNext.CreateInventoryItemInput
|
||||
| InventoryNext.CreateInventoryItemInput[],
|
||||
| InventoryTypes.CreateInventoryItemInput
|
||||
| InventoryTypes.CreateInventoryItemInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
InventoryNext.InventoryItemDTO | InventoryNext.InventoryItemDTO[]
|
||||
InventoryTypes.InventoryItemDTO | InventoryTypes.InventoryItemDTO[]
|
||||
> {
|
||||
const toCreate = this.sanitizeInventoryItemInput(
|
||||
Array.isArray(input) ? input : [input]
|
||||
@@ -353,7 +341,7 @@ export default class InventoryModuleService<
|
||||
)
|
||||
|
||||
const serializedItems = await this.baseRepository_.serialize<
|
||||
InventoryNext.InventoryItemDTO | InventoryNext.InventoryItemDTO[]
|
||||
InventoryTypes.InventoryItemDTO | InventoryTypes.InventoryItemDTO[]
|
||||
>(result, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -363,31 +351,31 @@ export default class InventoryModuleService<
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createInventoryItems_(
|
||||
input: InventoryNext.CreateInventoryItemInput[],
|
||||
input: InventoryTypes.CreateInventoryItemInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<InventoryNext.InventoryItemDTO[]> {
|
||||
): Promise<InventoryTypes.InventoryItemDTO[]> {
|
||||
return await this.inventoryItemService_.create(input)
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
createInventoryLevels(
|
||||
input: InventoryNext.CreateInventoryLevelInput,
|
||||
input: InventoryTypes.CreateInventoryLevelInput,
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryLevelDTO>
|
||||
): Promise<InventoryTypes.InventoryLevelDTO>
|
||||
createInventoryLevels(
|
||||
input: InventoryNext.CreateInventoryLevelInput[],
|
||||
input: InventoryTypes.CreateInventoryLevelInput[],
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryLevelDTO[]>
|
||||
): Promise<InventoryTypes.InventoryLevelDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async createInventoryLevels(
|
||||
input:
|
||||
| InventoryNext.CreateInventoryLevelInput[]
|
||||
| InventoryNext.CreateInventoryLevelInput,
|
||||
| InventoryTypes.CreateInventoryLevelInput[]
|
||||
| InventoryTypes.CreateInventoryLevelInput,
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
InventoryNext.InventoryLevelDTO[] | InventoryNext.InventoryLevelDTO
|
||||
InventoryTypes.InventoryLevelDTO[] | InventoryTypes.InventoryLevelDTO
|
||||
> {
|
||||
const toCreate = this.sanitizeInventoryLevelInput(
|
||||
Array.isArray(input) ? input : [input]
|
||||
@@ -407,7 +395,7 @@ export default class InventoryModuleService<
|
||||
)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
InventoryNext.InventoryLevelDTO[] | InventoryNext.InventoryLevelDTO
|
||||
InventoryTypes.InventoryLevelDTO[] | InventoryTypes.InventoryLevelDTO
|
||||
>(created, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -417,37 +405,31 @@ export default class InventoryModuleService<
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createInventoryLevels_(
|
||||
input: InventoryNext.CreateInventoryLevelInput[],
|
||||
input: InventoryTypes.CreateInventoryLevelInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<TInventoryLevel[]> {
|
||||
): Promise<InventoryLevel[]> {
|
||||
return await this.inventoryLevelService_.create(input, context)
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates inventory items
|
||||
* @param inventoryItemId - the id of the inventory item to update
|
||||
* @param input - the input object
|
||||
* @param context
|
||||
* @return The updated inventory item
|
||||
*/
|
||||
update(
|
||||
input: InventoryNext.UpdateInventoryItemInput[],
|
||||
// @ts-expect-error
|
||||
updateInventoryItems(
|
||||
input: InventoryTypes.UpdateInventoryItemInput[],
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryItemDTO[]>
|
||||
update(
|
||||
input: InventoryNext.UpdateInventoryItemInput,
|
||||
): Promise<InventoryTypes.InventoryItemDTO[]>
|
||||
updateInventoryItems(
|
||||
input: InventoryTypes.UpdateInventoryItemInput,
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryItemDTO>
|
||||
): Promise<InventoryTypes.InventoryItemDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async update(
|
||||
async updateInventoryItems(
|
||||
input:
|
||||
| InventoryNext.UpdateInventoryItemInput
|
||||
| InventoryNext.UpdateInventoryItemInput[],
|
||||
| InventoryTypes.UpdateInventoryItemInput
|
||||
| InventoryTypes.UpdateInventoryItemInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
InventoryNext.InventoryItemDTO | InventoryNext.InventoryItemDTO[]
|
||||
InventoryTypes.InventoryItemDTO | InventoryTypes.InventoryItemDTO[]
|
||||
> {
|
||||
const updates = this.sanitizeInventoryItemInput(
|
||||
Array.isArray(input) ? input : [input]
|
||||
@@ -467,7 +449,7 @@ export default class InventoryModuleService<
|
||||
)
|
||||
|
||||
const serializedItems = await this.baseRepository_.serialize<
|
||||
InventoryNext.InventoryItemDTO | InventoryNext.InventoryItemDTO[]
|
||||
InventoryTypes.InventoryItemDTO | InventoryTypes.InventoryItemDTO[]
|
||||
>(result, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -477,9 +459,11 @@ export default class InventoryModuleService<
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateInventoryItems_(
|
||||
input: (Partial<InventoryNext.CreateInventoryItemInput> & { id: string })[],
|
||||
input: (Partial<InventoryTypes.CreateInventoryItemInput> & {
|
||||
id: string
|
||||
})[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<TInventoryItem[]> {
|
||||
): Promise<InventoryItem[]> {
|
||||
return await this.inventoryItemService_.update(input, context)
|
||||
}
|
||||
|
||||
@@ -546,11 +530,11 @@ export default class InventoryModuleService<
|
||||
async updateInventoryLevels(
|
||||
updates: InventoryTypes.BulkUpdateInventoryLevelInput[],
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryLevelDTO[]>
|
||||
): Promise<InventoryTypes.InventoryLevelDTO[]>
|
||||
async updateInventoryLevels(
|
||||
updates: InventoryTypes.BulkUpdateInventoryLevelInput,
|
||||
context?: Context
|
||||
): Promise<InventoryNext.InventoryLevelDTO>
|
||||
): Promise<InventoryTypes.InventoryLevelDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
@@ -560,7 +544,7 @@ export default class InventoryModuleService<
|
||||
| InventoryTypes.BulkUpdateInventoryLevelInput,
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
InventoryNext.InventoryLevelDTO | InventoryNext.InventoryLevelDTO[]
|
||||
InventoryTypes.InventoryLevelDTO | InventoryTypes.InventoryLevelDTO[]
|
||||
> {
|
||||
const input = this.sanitizeInventoryLevelInput(
|
||||
Array.isArray(updates) ? updates : [updates]
|
||||
@@ -580,8 +564,7 @@ export default class InventoryModuleService<
|
||||
)
|
||||
|
||||
const updatedLevels = await this.baseRepository_.serialize<
|
||||
| InventoryTypes.InventoryNext.InventoryLevelDTO
|
||||
| InventoryTypes.InventoryNext.InventoryLevelDTO[]
|
||||
InventoryTypes.InventoryLevelDTO | InventoryTypes.InventoryLevelDTO[]
|
||||
>(levels, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -631,23 +614,23 @@ export default class InventoryModuleService<
|
||||
*/
|
||||
// @ts-ignore
|
||||
async updateReservationItems(
|
||||
input: InventoryNext.UpdateReservationItemInput[],
|
||||
input: InventoryTypes.UpdateReservationItemInput[],
|
||||
context?: Context
|
||||
): Promise<InventoryNext.ReservationItemDTO[]>
|
||||
): Promise<InventoryTypes.ReservationItemDTO[]>
|
||||
async updateReservationItems(
|
||||
input: InventoryNext.UpdateReservationItemInput,
|
||||
input: InventoryTypes.UpdateReservationItemInput,
|
||||
context?: Context
|
||||
): Promise<InventoryNext.ReservationItemDTO>
|
||||
): Promise<InventoryTypes.ReservationItemDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async updateReservationItems(
|
||||
input:
|
||||
| InventoryNext.UpdateReservationItemInput
|
||||
| InventoryNext.UpdateReservationItemInput[],
|
||||
| InventoryTypes.UpdateReservationItemInput
|
||||
| InventoryTypes.UpdateReservationItemInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
InventoryNext.ReservationItemDTO | InventoryNext.ReservationItemDTO[]
|
||||
InventoryTypes.ReservationItemDTO | InventoryTypes.ReservationItemDTO[]
|
||||
> {
|
||||
const update = Array.isArray(input) ? input : [input]
|
||||
const result = await this.updateReservationItems_(update, context)
|
||||
@@ -664,7 +647,7 @@ export default class InventoryModuleService<
|
||||
)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
InventoryNext.ReservationItemDTO | InventoryNext.ReservationItemDTO[]
|
||||
InventoryTypes.ReservationItemDTO | InventoryTypes.ReservationItemDTO[]
|
||||
>(result, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -674,9 +657,9 @@ export default class InventoryModuleService<
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateReservationItems_(
|
||||
input: (InventoryNext.UpdateReservationItemInput & { id: string })[],
|
||||
input: (InventoryTypes.UpdateReservationItemInput & { id: string })[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<TReservationItem[]> {
|
||||
): Promise<ReservationItem[]> {
|
||||
const ids = input.map((u) => u.id)
|
||||
const reservationItems = await this.listReservationItems(
|
||||
{ id: ids },
|
||||
@@ -797,7 +780,7 @@ export default class InventoryModuleService<
|
||||
locationId: string | string[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<void> {
|
||||
const reservations: InventoryNext.ReservationItemDTO[] =
|
||||
const reservations: InventoryTypes.ReservationItemDTO[] =
|
||||
await this.listReservationItems({ location_id: locationId }, {}, context)
|
||||
|
||||
await this.reservationItemService_.softDelete(
|
||||
@@ -834,7 +817,7 @@ export default class InventoryModuleService<
|
||||
lineItemId: string | string[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<void> {
|
||||
const reservations: InventoryNext.ReservationItemDTO[] =
|
||||
const reservations: InventoryTypes.ReservationItemDTO[] =
|
||||
await this.listReservationItems({ line_item_id: lineItemId }, {}, context)
|
||||
|
||||
await this.reservationItemService_.softDelete(
|
||||
@@ -871,7 +854,7 @@ export default class InventoryModuleService<
|
||||
lineItemId: string | string[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<void> {
|
||||
const reservations: InventoryNext.ReservationItemDTO[] =
|
||||
const reservations: InventoryTypes.ReservationItemDTO[] =
|
||||
await this.listReservationItems({ line_item_id: lineItemId }, {}, context)
|
||||
|
||||
await this.reservationItemService_.restore(
|
||||
@@ -910,7 +893,7 @@ export default class InventoryModuleService<
|
||||
locationId: string,
|
||||
adjustment: number,
|
||||
context: Context
|
||||
): Promise<InventoryNext.InventoryLevelDTO>
|
||||
): Promise<InventoryTypes.InventoryLevelDTO>
|
||||
|
||||
adjustInventory(
|
||||
data: {
|
||||
@@ -919,7 +902,7 @@ export default class InventoryModuleService<
|
||||
adjustment: number
|
||||
}[],
|
||||
context: Context
|
||||
): Promise<InventoryNext.InventoryLevelDTO[]>
|
||||
): Promise<InventoryTypes.InventoryLevelDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
@@ -929,7 +912,7 @@ export default class InventoryModuleService<
|
||||
adjustment?: number,
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
InventoryNext.InventoryLevelDTO | InventoryNext.InventoryLevelDTO[]
|
||||
InventoryTypes.InventoryLevelDTO | InventoryTypes.InventoryLevelDTO[]
|
||||
> {
|
||||
let all: any = inventoryItemIdOrData
|
||||
|
||||
@@ -943,7 +926,7 @@ export default class InventoryModuleService<
|
||||
]
|
||||
}
|
||||
|
||||
const results: TInventoryLevel[] = []
|
||||
const results: InventoryLevel[] = []
|
||||
|
||||
for (const data of all) {
|
||||
const result = await this.adjustInventory_(
|
||||
@@ -964,7 +947,7 @@ export default class InventoryModuleService<
|
||||
})
|
||||
}
|
||||
|
||||
return await this.baseRepository_.serialize<InventoryNext.InventoryLevelDTO>(
|
||||
return await this.baseRepository_.serialize<InventoryTypes.InventoryLevelDTO>(
|
||||
Array.isArray(inventoryItemIdOrData) ? results : results[0],
|
||||
{
|
||||
populate: true,
|
||||
@@ -978,7 +961,7 @@ export default class InventoryModuleService<
|
||||
locationId: string,
|
||||
adjustment: number,
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<TInventoryLevel> {
|
||||
): Promise<InventoryLevel> {
|
||||
const inventoryLevel = await this.retrieveInventoryLevelByItemAndLocation(
|
||||
inventoryItemId,
|
||||
locationId,
|
||||
@@ -1001,7 +984,7 @@ export default class InventoryModuleService<
|
||||
inventoryItemId: string,
|
||||
locationId: string,
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<InventoryNext.InventoryLevelDTO> {
|
||||
): Promise<InventoryTypes.InventoryLevelDTO> {
|
||||
const [inventoryLevel] = await this.listInventoryLevels(
|
||||
{ inventory_item_id: inventoryItemId, location_id: locationId },
|
||||
{ take: 1 },
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"access": "public"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -24,12 +24,18 @@ export const CartPaymentCollection: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "cart_id",
|
||||
alias: "cart",
|
||||
args: {
|
||||
methodSuffix: "Carts",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.PAYMENT,
|
||||
primaryKey: "id",
|
||||
foreignKey: "payment_collection_id",
|
||||
alias: "payment_collection",
|
||||
args: {
|
||||
methodSuffix: "PaymentCollections",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -24,12 +24,18 @@ export const CartPromotion: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "cart_id",
|
||||
alias: "cart",
|
||||
args: {
|
||||
methodSuffix: "Carts",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.PROMOTION,
|
||||
primaryKey: "id",
|
||||
foreignKey: "promotion_id",
|
||||
alias: "promotions",
|
||||
args: {
|
||||
methodSuffix: "Promotions",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -24,12 +24,18 @@ export const LocationFulfillmentSet: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "stock_location_id",
|
||||
alias: "location",
|
||||
args: {
|
||||
methodSuffix: "StockLocations",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.FULFILLMENT,
|
||||
primaryKey: "id",
|
||||
foreignKey: "fulfillment_set_id",
|
||||
alias: "fulfillment_set",
|
||||
args: {
|
||||
methodSuffix: "FulfillmentSets",
|
||||
},
|
||||
deleteCascade: true,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -24,12 +24,18 @@ export const OrderCart: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "order_id",
|
||||
alias: "order",
|
||||
args: {
|
||||
methodSuffix: "Orders",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.CART,
|
||||
primaryKey: "id",
|
||||
foreignKey: "cart_id",
|
||||
alias: "cart",
|
||||
args: {
|
||||
methodSuffix: "Carts",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -24,6 +24,9 @@ export const OrderFulfillment: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "order_id",
|
||||
alias: "order",
|
||||
args: {
|
||||
methodSuffix: "Orders",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.FULFILLMENT,
|
||||
|
||||
@@ -24,12 +24,18 @@ export const OrderPaymentCollection: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "order_id",
|
||||
alias: "order",
|
||||
args: {
|
||||
methodSuffix: "Orders",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.PAYMENT,
|
||||
primaryKey: "id",
|
||||
foreignKey: "payment_collection_id",
|
||||
alias: "payment_collection",
|
||||
args: {
|
||||
methodSuffix: "PaymentCollections",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -24,12 +24,18 @@ export const OrderPromotion: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "order_id",
|
||||
alias: "order",
|
||||
args: {
|
||||
methodSuffix: "Orders",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.PROMOTION,
|
||||
primaryKey: "id",
|
||||
foreignKey: "promotion_id",
|
||||
alias: "promotion",
|
||||
args: {
|
||||
methodSuffix: "Promotions",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -24,12 +24,18 @@ export const ProductSalesChannel: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "product_id",
|
||||
alias: "product",
|
||||
args: {
|
||||
methodSuffix: "Products",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.SALES_CHANNEL,
|
||||
primaryKey: "id",
|
||||
foreignKey: "sales_channel_id",
|
||||
alias: "sales_channel",
|
||||
args: {
|
||||
methodSuffix: "SalesChannels",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -34,7 +34,7 @@ export const ProductVariantInventoryItem: ModuleJoinerConfig = {
|
||||
foreignKey: "variant_id",
|
||||
alias: "variant",
|
||||
args: {
|
||||
methodSuffix: "Variants",
|
||||
methodSuffix: "ProductVariants",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -42,6 +42,9 @@ export const ProductVariantInventoryItem: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "inventory_item_id",
|
||||
alias: "inventory",
|
||||
args: {
|
||||
methodSuffix: "InventoryItems",
|
||||
},
|
||||
deleteCascade: true,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -25,7 +25,7 @@ export const ProductVariantPriceSet: ModuleJoinerConfig = {
|
||||
foreignKey: "variant_id",
|
||||
alias: "variant",
|
||||
args: {
|
||||
methodSuffix: "Variants",
|
||||
methodSuffix: "ProductVariants",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -33,6 +33,9 @@ export const ProductVariantPriceSet: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "price_set_id",
|
||||
alias: "price_set",
|
||||
args: {
|
||||
methodSuffix: "PriceSets",
|
||||
},
|
||||
deleteCascade: true,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -24,12 +24,18 @@ export const PublishableApiKeySalesChannel: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "publishable_key_id",
|
||||
alias: "api_key",
|
||||
args: {
|
||||
methodSuffix: "ApiKeys",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.SALES_CHANNEL,
|
||||
primaryKey: "id",
|
||||
foreignKey: "sales_channel_id",
|
||||
alias: "sales_channel",
|
||||
args: {
|
||||
methodSuffix: "SalesChannels",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -12,6 +12,9 @@ export const CartCustomer: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "customer_id",
|
||||
alias: "customer",
|
||||
args: {
|
||||
methodSuffix: "Customers",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -21,6 +24,9 @@ export const CartCustomer: ModuleJoinerConfig = {
|
||||
primaryKey: "customer_id",
|
||||
foreignKey: "id",
|
||||
alias: "carts",
|
||||
args: {
|
||||
methodSuffix: "Carts",
|
||||
},
|
||||
isList: true,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -12,6 +12,9 @@ export const CartProduct: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "items.product_id",
|
||||
alias: "product",
|
||||
args: {
|
||||
methodSuffix: "Products",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -22,7 +25,7 @@ export const CartProduct: ModuleJoinerConfig = {
|
||||
foreignKey: "items.variant_id",
|
||||
alias: "variant",
|
||||
args: {
|
||||
methodSuffix: "Variants",
|
||||
methodSuffix: "ProductVariants",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -12,6 +12,9 @@ export const CartRegion: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "region_id",
|
||||
alias: "region",
|
||||
args: {
|
||||
methodSuffix: "Regions",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -21,6 +24,9 @@ export const CartRegion: ModuleJoinerConfig = {
|
||||
primaryKey: "region_id",
|
||||
foreignKey: "id",
|
||||
alias: "carts",
|
||||
args: {
|
||||
methodSuffix: "Carts",
|
||||
},
|
||||
isList: true,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -12,6 +12,9 @@ export const CartSalesChannel: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "sales_channel_id",
|
||||
alias: "sales_channel",
|
||||
args: {
|
||||
methodSuffix: "SalesChannels",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -21,6 +24,9 @@ export const CartSalesChannel: ModuleJoinerConfig = {
|
||||
primaryKey: "sales_channel_id",
|
||||
foreignKey: "id",
|
||||
alias: "carts",
|
||||
args: {
|
||||
methodSuffix: "Carts",
|
||||
},
|
||||
isList: true,
|
||||
},
|
||||
},
|
||||
|
||||
+3
@@ -12,6 +12,9 @@ export const InventoryLevelStockLocation: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "location_id",
|
||||
alias: "stock_locations",
|
||||
args: {
|
||||
methodSuffix: "StockLocations",
|
||||
},
|
||||
isList: true,
|
||||
},
|
||||
},
|
||||
|
||||
+3
@@ -12,6 +12,9 @@ export const LineItemAdjustmentPromotion: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "promotion_id",
|
||||
alias: "promotion",
|
||||
args: {
|
||||
methodSuffix: "Promotions",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -12,6 +12,9 @@ export const OrderCustomer: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "customer_id",
|
||||
alias: "customer",
|
||||
args: {
|
||||
methodSuffix: "Customers",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -21,6 +24,9 @@ export const OrderCustomer: ModuleJoinerConfig = {
|
||||
primaryKey: "customer_id",
|
||||
foreignKey: "id",
|
||||
alias: "orders",
|
||||
args: {
|
||||
methodSuffix: "Orders",
|
||||
},
|
||||
isList: true,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -12,6 +12,9 @@ export const OrderProduct: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "items.product_id",
|
||||
alias: "product",
|
||||
args: {
|
||||
methodSuffix: "Products",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -22,7 +25,7 @@ export const OrderProduct: ModuleJoinerConfig = {
|
||||
foreignKey: "items.variant_id",
|
||||
alias: "variant",
|
||||
args: {
|
||||
methodSuffix: "Variants",
|
||||
methodSuffix: "ProductVariants",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -12,6 +12,9 @@ export const OrderRegion: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "region_id",
|
||||
alias: "region",
|
||||
args: {
|
||||
methodSuffix: "Regions",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -21,6 +24,9 @@ export const OrderRegion: ModuleJoinerConfig = {
|
||||
primaryKey: "region_id",
|
||||
foreignKey: "id",
|
||||
alias: "orders",
|
||||
args: {
|
||||
methodSuffix: "Orders",
|
||||
},
|
||||
isList: true,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -13,6 +13,9 @@ export const OrderSalesChannel: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "sales_channel_id",
|
||||
alias: "sales_channel",
|
||||
args: {
|
||||
methodSuffix: "SalesChannels",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -22,6 +25,9 @@ export const OrderSalesChannel: ModuleJoinerConfig = {
|
||||
primaryKey: "sales_channel_id",
|
||||
foreignKey: "id",
|
||||
alias: "orders",
|
||||
args: {
|
||||
methodSuffix: "Orders",
|
||||
},
|
||||
isList: true,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -12,6 +12,9 @@ export const StoreDefaultCurrency: ModuleJoinerConfig = {
|
||||
primaryKey: "code",
|
||||
foreignKey: "default_currency_code",
|
||||
alias: "default_currency",
|
||||
args: {
|
||||
methodSuffix: "Currencies",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -24,6 +24,9 @@ export const RegionPaymentProvider: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "region_id",
|
||||
alias: "region",
|
||||
args: {
|
||||
methodSuffix: "Regions",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.PAYMENT,
|
||||
|
||||
@@ -24,12 +24,18 @@ export const SalesChannelLocation: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "sales_channel_id",
|
||||
alias: "sales_channel",
|
||||
args: {
|
||||
methodSuffix: "SalesChannels",
|
||||
},
|
||||
},
|
||||
{
|
||||
serviceName: Modules.STOCK_LOCATION,
|
||||
primaryKey: "id",
|
||||
foreignKey: "stock_location_id",
|
||||
alias: "location",
|
||||
args: {
|
||||
methodSuffix: "StockLocations",
|
||||
},
|
||||
},
|
||||
],
|
||||
extends: [
|
||||
|
||||
@@ -33,6 +33,9 @@ export const ShippingOptionPriceSet: ModuleJoinerConfig = {
|
||||
primaryKey: "id",
|
||||
foreignKey: "price_set_id",
|
||||
alias: "price_set",
|
||||
args: {
|
||||
methodSuffix: "PriceSets",
|
||||
},
|
||||
deleteCascade: true,
|
||||
},
|
||||
],
|
||||
|
||||
+3
-3
@@ -40,7 +40,7 @@ moduleIntegrationTestRunner({
|
||||
data: {},
|
||||
}
|
||||
|
||||
const result = await service.create(notification)
|
||||
const result = await service.createNotifications(notification)
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_id: "test-provider",
|
||||
@@ -58,7 +58,7 @@ moduleIntegrationTestRunner({
|
||||
idempotency_key: "idempotency-key",
|
||||
}
|
||||
|
||||
const result = await service.create(notification)
|
||||
const result = await service.createNotifications(notification)
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_id: "test-provider",
|
||||
@@ -66,7 +66,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
)
|
||||
|
||||
const secondResult = await service.create(notification)
|
||||
const secondResult = await service.createNotifications(notification)
|
||||
expect(secondResult).toBe(undefined)
|
||||
})
|
||||
}),
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user