Feat: Bulk add customers to customer group (#1095)

* fix babel transform-runtime regenerator required for migrations

* add customer group model

* add migration for customer group

* add customer group model export

* add customer group repository

* add customer group service

* add CustomerGroupRepository to "withTransaction" in CustomerGroupService

* remove unnecessary argument to runtime plugin

* service export ordering

* add create customer group endpoint

* add customergroup to route index in admin

* add customer group service

* add customer groups test

* cleanup

* add customers batch initial

* batch creation of customer groups

* integration testing batch creation

* integration tests

* chaining existing customers creation in repo

* remove commented test

* update unit tests to reflect change in idempotent behavior

* ensure that exceptions are expected

* update idempotency behavior

* update formatting

* update format

* Update packages/medusa/src/repositories/customer-group.ts

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>

* pr feedback

* add In import

* add seperate model dto

* add integration test

* error handling in repository

* remove unused import

* jsdoc

* Update packages/medusa/src/api/routes/admin/customer-groups/add-customers-batch.ts

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>

* Update packages/medusa/src/api/routes/admin/customer-groups/add-customers-batch.ts

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>

* pr review comments

* rename variable

* fix: adds atomic phase clean up callback

* fix: call error handler in new transaction block too

* restore

* error handling

* fix: error handler in no isolation case

* add integration test for missing group on update

* final adjustments to test

* fix pr feedback

* cleanup core for pr

* remove console.log

* remove customergroupservice test from customers

* Apply suggestions from code review

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>

* add end bracket to customer tests

* remove comments

* change model decorator

* fix integration test merge

* onDelete cascade instead of cascade:true

* remove verbose flag

* fix: dedupe type

* add save to customer groups

* customer model delete cascade

* add await to asyncronous save operations

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
Philip Korsholm
2022-02-28 10:03:26 +01:00
committed by GitHub
co-authored by Sebastian Rindom
parent 562a1b427a
commit 4b4463f0e2
21 changed files with 638 additions and 251 deletions
@@ -46,7 +46,13 @@ export default () => {
statusCode = 500
errObj.code = API_ERROR
break
case MedusaError.Types.UNEXPECTED_STATE:
case MedusaError.Types.INVALID_ARGUMENT:
break
default:
errObj.code = "unknown_error"
errObj.message = "An unknown error occurred."
errObj.type = "unknown_error"
break
}
@@ -33,9 +33,10 @@ describe("POST /admin/collections/:id/products/batch", () => {
it("product collection service update", () => {
expect(ProductCollectionServiceMock.addProducts).toHaveBeenCalledTimes(1)
expect(
ProductCollectionServiceMock.addProducts
).toHaveBeenCalledWith(IdMap.getId("col"), ["prod_1", "prod_2"])
expect(ProductCollectionServiceMock.addProducts).toHaveBeenCalledWith(
IdMap.getId("col"),
["prod_1", "prod_2"]
)
})
})
@@ -60,7 +61,9 @@ describe("POST /admin/collections/:id/products/batch", () => {
})
it("throws error", () => {
expect(subject.body.message).toBe("Product collection not found")
expect(subject.body.message).toBe(
"Product collection with id: null was not found"
)
})
})
@@ -0,0 +1,51 @@
import { Type } from "class-transformer"
import { ValidateNested } from "class-validator"
import { CustomerGroupService } from "../../../../services"
import { CustomerGroupsBatchCustomer } from "../../../../types/customer-groups"
import { validator } from "../../../../utils/validator"
/**
* @oas [post] /customer-groups/{id}/customers/batch
* operationId: "PostCustomerGroupsGroupCustomersBatch"
* summary: "Add a list of customers to a customer group "
* description: "Adds a list of customers, represented by id's, to a customer group."
* x-authenticated: true
* parameters:
* - (path) id=* {string} The id of the customer group.
* - (body) customers=* {{id: string }[]} ids of the customers to add
* tags:
* - CustomerGroup
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* customerGroup:
* $ref: "#/components/schemas/customergroup"
*/
export default async (req, res) => {
const { id } = req.params
const validated = await validator(
AdminPostCustomerGroupsGroupCustomersBatchReq,
req.body
)
const customerGroupService: CustomerGroupService = req.scope.resolve(
"customerGroupService"
)
const customer_group = await customerGroupService.addCustomers(
id,
validated.customer_ids.map(({ id }) => id)
)
res.status(200).json({ customer_group })
}
export class AdminPostCustomerGroupsGroupCustomersBatchReq {
@ValidateNested({ each: true })
@Type(() => CustomerGroupsBatchCustomer)
customer_ids: CustomerGroupsBatchCustomer[]
}
@@ -10,6 +10,10 @@ export default (app) => {
route.get("/:id", middlewares.wrap(require("./get-customer-group").default))
route.post("/", middlewares.wrap(require("./create-customer-group").default))
route.post(
"/:id/customers/batch",
middlewares.wrap(require("./add-customers-batch").default)
)
route.delete(
"/:id/customers/batch",
middlewares.wrap(require("./delete-customers-batch").default)