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:
authored and GitHub committed 2024-06-19 13:02:16 +00:00
1 parent 2895ccfba8
commit 48963f55ef
533 files changed
+6469 -9769

No files matched your search

@@ -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",
})
@@ -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([
@@ -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",
})
@@ -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",
})
@@ -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)
})
@@ -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",
})
@@ -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",
})
+1 -4
View File
@@ -8,10 +8,7 @@
"dist"
],
"engines": {
"node": ">=16"
},
"bin": {
"medusa-fulfillment-seed": "dist/scripts/bin/run-seed.js"
"node": ">=20"
},
"repository": {
"type": "git",
+12 -4
View File
@@ -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,
{