feat: Update service zone (#6990)

This commit is contained in:
Oli Juhl authored and GitHub committed 2024-04-07 17:30:55 +02:00
1 parent b6879f017a
commit a24d6e6c97
16 files changed
+576 -194

No files matched your search

@@ -284,7 +284,6 @@ moduleIntegrationTestRunner({
)
const updateData = {
id: createdServiceZone.id,
name: "updated-service-zone-test",
geo_zones: [
{
@@ -296,12 +295,13 @@ moduleIntegrationTestRunner({
}
const updatedServiceZone = await service.updateServiceZones(
createdServiceZone.id,
updateData
)
expect(updatedServiceZone).toEqual(
expect.objectContaining({
id: updateData.id,
id: createdServiceZone.id,
name: updateData.name,
geo_zones: expect.arrayContaining([
expect.objectContaining({
@@ -314,7 +314,60 @@ moduleIntegrationTestRunner({
)
})
it("should update a collection of service zones", async function () {
it("should fail on duplicated service zone name", async function () {
const fulfillmentSet = await service.create({
name: "test",
type: "test-type",
})
const createData: CreateServiceZoneDTO[] = [
{
name: "service-zone-test",
fulfillment_set_id: fulfillmentSet.id,
geo_zones: [
{
type: GeoZoneType.COUNTRY,
country_code: "fr",
},
],
},
{
name: "service-zone-test2",
fulfillment_set_id: fulfillmentSet.id,
geo_zones: [
{
type: GeoZoneType.COUNTRY,
country_code: "us",
},
],
},
]
const createdServiceZones = await service.createServiceZones(
createData
)
const updateData: UpdateServiceZoneDTO = {
name: "service-zone-test",
geo_zones: [
{
type: GeoZoneType.COUNTRY,
country_code: "us",
},
],
}
const err = await service
.updateServiceZones(createdServiceZones[1].id, updateData)
.catch((e) => e)
expect(err).toBeDefined()
expect(err.message).toContain("exists")
})
})
describe("on upsert", () => {
it("should upsert a collection of service zones", async function () {
const fulfillmentSet = await service.create({
name: "test",
type: "test-type",
@@ -360,7 +413,7 @@ moduleIntegrationTestRunner({
})
)
const updatedServiceZones = await service.updateServiceZones(
const updatedServiceZones = await service.upsertServiceZones(
updateData
)
@@ -385,58 +438,6 @@ moduleIntegrationTestRunner({
)
}
})
it("should fail on duplicated service zone name", async function () {
const fulfillmentSet = await service.create({
name: "test",
type: "test-type",
})
const createData: CreateServiceZoneDTO[] = [
{
name: "service-zone-test",
fulfillment_set_id: fulfillmentSet.id,
geo_zones: [
{
type: GeoZoneType.COUNTRY,
country_code: "fr",
},
],
},
{
name: "service-zone-test2",
fulfillment_set_id: fulfillmentSet.id,
geo_zones: [
{
type: GeoZoneType.COUNTRY,
country_code: "us",
},
],
},
]
const createdServiceZones = await service.createServiceZones(
createData
)
const updateData: UpdateServiceZoneDTO = {
id: createdServiceZones[1].id,
name: "service-zone-test",
geo_zones: [
{
type: GeoZoneType.COUNTRY,
country_code: "us",
},
],
}
const err = await service
.updateServiceZones(updateData)
.catch((e) => e)
expect(err).toBeDefined()
expect(err.message).toContain("exists")
})
})
})
})
@@ -1,3 +1,4 @@
import { Modules } from "@medusajs/modules-sdk"
import {
Context,
DAL,
@@ -11,20 +12,21 @@ import {
ModulesSdkTypes,
ShippingOptionDTO,
UpdateFulfillmentSetDTO,
UpdateServiceZoneDTO,
} from "@medusajs/types"
import {
arrayDifference,
EmitEvents,
FulfillmentUtils,
getSetDifference,
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
arrayDifference,
getSetDifference,
isString,
promiseAll,
} from "@medusajs/utils"
import {
Fulfillment,
FulfillmentSet,
@@ -38,7 +40,6 @@ import {
import { isContextValid, validateRules } from "@utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import FulfillmentProviderService from "./fulfillment-provider"
import { Modules } from "@medusajs/modules-sdk"
const generateMethodForModels = [
ServiceZone,
@@ -791,31 +792,56 @@ export default class FulfillmentModuleService<
}
updateServiceZones(
data: FulfillmentTypes.UpdateServiceZoneDTO[],
sharedContext?: Context
): Promise<FulfillmentTypes.ServiceZoneDTO[]>
updateServiceZones(
id: string,
data: FulfillmentTypes.UpdateServiceZoneDTO,
sharedContext?: Context
): Promise<FulfillmentTypes.ServiceZoneDTO>
updateServiceZones(
selector: FulfillmentTypes.FilterableServiceZoneProps,
data: FulfillmentTypes.UpdateServiceZoneDTO,
sharedContext?: Context
): Promise<FulfillmentTypes.ServiceZoneDTO[]>
@InjectManager("baseRepository_")
async updateServiceZones(
data:
| FulfillmentTypes.UpdateServiceZoneDTO[]
| FulfillmentTypes.UpdateServiceZoneDTO,
idOrSelector: string | FulfillmentTypes.FilterableServiceZoneProps,
data: FulfillmentTypes.UpdateServiceZoneDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<
FulfillmentTypes.ServiceZoneDTO[] | FulfillmentTypes.ServiceZoneDTO
> {
const normalizedInput: UpdateServiceZoneDTO[] = []
if (isString(idOrSelector)) {
normalizedInput.push({ id: idOrSelector, ...data })
} else {
const serviceZones = await this.serviceZoneService_.list(
{ ...idOrSelector },
{},
sharedContext
)
if (!serviceZones.length) {
return []
}
for (const serviceZone of serviceZones) {
normalizedInput.push({ id: serviceZone.id, ...data })
}
}
const updatedServiceZones = await this.updateServiceZones_(
data,
normalizedInput,
sharedContext
)
const toReturn = isString(idOrSelector)
? updatedServiceZones[0]
: updatedServiceZones
return await this.baseRepository_.serialize<
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[]
>(updatedServiceZones, {
>(toReturn, {
populate: true,
})
}
@@ -873,7 +899,7 @@ export default class FulfillmentModuleService<
data_.forEach((serviceZone) => {
if (serviceZone.geo_zones) {
const existingServiceZone = serviceZoneMap.get(serviceZone.id)!
const existingServiceZone = serviceZoneMap.get(serviceZone.id!)!
const existingGeoZones = existingServiceZone.geo_zones
const updatedGeoZones = serviceZone.geo_zones
const toDeleteGeoZoneIds = getSetDifference(
@@ -920,7 +946,9 @@ export default class FulfillmentModuleService<
FulfillmentModuleService.validateGeoZones([geoZone])
return geoZone
}
return geoZonesMap.get(geoZone.id)!
const existing = geoZonesMap.get(geoZone.id)!
return { ...existing, ...geoZone }
})
}
})
@@ -942,6 +970,81 @@ export default class FulfillmentModuleService<
return Array.isArray(data) ? updatedServiceZones : updatedServiceZones[0]
}
upsertServiceZones(
data: FulfillmentTypes.UpsertServiceZoneDTO,
sharedContext?: Context
): Promise<FulfillmentTypes.ServiceZoneDTO>
upsertServiceZones(
data: FulfillmentTypes.UpsertServiceZoneDTO[],
sharedContext?: Context
): Promise<FulfillmentTypes.ServiceZoneDTO[]>
@InjectManager("baseRepository_")
async upsertServiceZones(
data:
| FulfillmentTypes.UpsertServiceZoneDTO
| FulfillmentTypes.UpsertServiceZoneDTO[],
sharedContext?: Context
): Promise<
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[]
> {
const upsertServiceZones = await this.upsertServiceZones_(
data,
sharedContext
)
const allServiceZones = await this.baseRepository_.serialize<
FulfillmentTypes.ServiceZoneDTO[] | FulfillmentTypes.ServiceZoneDTO
>(upsertServiceZones)
return Array.isArray(data) ? allServiceZones : allServiceZones[0]
}
@InjectTransactionManager("baseRepository_")
async upsertServiceZones_(
data:
| FulfillmentTypes.UpsertServiceZoneDTO[]
| FulfillmentTypes.UpsertServiceZoneDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<TServiceZoneEntity[] | TServiceZoneEntity> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter(
(serviceZone): serviceZone is FulfillmentTypes.UpdateServiceZoneDTO =>
!!serviceZone.id
)
const forCreate = input.filter(
(serviceZone): serviceZone is FulfillmentTypes.CreateServiceZoneDTO =>
!serviceZone.id
)
const created: TServiceZoneEntity[] = []
const updated: TServiceZoneEntity[] = []
if (forCreate.length) {
const createdServiceZones = await this.createServiceZones_(
forCreate,
sharedContext
)
const toPush = Array.isArray(createdServiceZones)
? createdServiceZones
: [createdServiceZones]
created.push(...toPush)
}
if (forUpdate.length) {
const updatedServiceZones = await this.updateServiceZones_(
forUpdate,
sharedContext
)
const toPush = Array.isArray(updatedServiceZones)
? updatedServiceZones
: [updatedServiceZones]
updated.push(...toPush)
}
return [...created, ...updated]
}
updateShippingOptions(
data: FulfillmentTypes.UpdateShippingOptionDTO[],
sharedContext?: Context