Feat: Delete customer group (#1102)
This commit is contained in:
committed by
olivermrbl
parent
a9f516ead2
commit
44fb9531d2
@@ -86,6 +86,113 @@ describe("/admin/customer-groups", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/customer-groups", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
await customerSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("removes customer group from get endpoint", async () => {
|
||||
expect.assertions(3)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const id = "customer-group-1"
|
||||
|
||||
const deleteResponse = await api.delete(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
|
||||
expect(deleteResponse.data).toEqual({
|
||||
id: id,
|
||||
object: "customer_group",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
await api
|
||||
.get(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((error) => {
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
`CustomerGroup with ${id} was not found`
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it("removes customer group from customer upon deletion", async () => {
|
||||
expect.assertions(3)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const id = "test-group-delete"
|
||||
|
||||
const customerRes_preDeletion = await api.get(
|
||||
`/admin/customers/test-customer-delete-cg?expand=groups`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(customerRes_preDeletion.data.customer).toEqual(
|
||||
expect.objectContaining({
|
||||
groups: [
|
||||
expect.objectContaining({
|
||||
id: "test-group-delete",
|
||||
name: "test-group-delete",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
|
||||
const deleteResponse = await api
|
||||
.delete(`/admin/customer-groups/${id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(deleteResponse.data).toEqual({
|
||||
id: id,
|
||||
object: "customer_group",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
const customerRes = await api.get(
|
||||
`/admin/customers/test-customer-delete-cg?expand=groups`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(customerRes.data.customer).toEqual(
|
||||
expect.objectContaining({
|
||||
groups: [],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/customer-groups", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
|
||||
@@ -56,7 +56,7 @@ describe("/admin/customers", () => {
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(5)
|
||||
expect(response.data.count).toEqual(6)
|
||||
expect(response.data.customers).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
|
||||
@@ -42,6 +42,11 @@ module.exports = async (connection, data = {}) => {
|
||||
groups: [{ id: "test-group-5", name: "test-group-5" }],
|
||||
})
|
||||
|
||||
const deletionCustomer = await manager.create(Customer, {
|
||||
id: "test-customer-delete-cg",
|
||||
email: "test-deletetion-cg@email.com",
|
||||
})
|
||||
|
||||
await manager.insert(CustomerGroup, {
|
||||
id: "customer-group-1",
|
||||
name: "vip-customers",
|
||||
@@ -56,4 +61,12 @@ module.exports = async (connection, data = {}) => {
|
||||
id: "test-group-5",
|
||||
name: "test-group-5",
|
||||
})
|
||||
|
||||
const c_group_delete = manager.create(CustomerGroup, {
|
||||
id: "test-group-delete",
|
||||
name: "test-group-delete",
|
||||
})
|
||||
|
||||
deletionCustomer.groups = [c_group_delete]
|
||||
await manager.save(deletionCustomer)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { CustomerGroupService } from "../../../../services"
|
||||
|
||||
/**
|
||||
* @oas [delete] /customer-groups/{id}
|
||||
* operationId: "DeleteCustomerGroupsCustomerGroup"
|
||||
* summary: "Delete a CustomerGroup"
|
||||
* description: "Deletes a CustomerGroup."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (path) id=* {string} The id of the Customer Group
|
||||
* tags:
|
||||
* - CustomerGroup
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* id:
|
||||
* type: string
|
||||
* description: The id of the deleted customer group.
|
||||
* object:
|
||||
* type: string
|
||||
* description: The type of the object that was deleted.
|
||||
* deleted:
|
||||
* type: boolean
|
||||
*/
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const customerGroupService: CustomerGroupService = req.scope.resolve(
|
||||
"customerGroupService"
|
||||
)
|
||||
|
||||
await customerGroupService.delete(id)
|
||||
|
||||
res.json({
|
||||
id: id,
|
||||
object: "customer_group",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -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.delete(
|
||||
"/:id",
|
||||
middlewares.wrap(require("./delete-customer-group").default)
|
||||
)
|
||||
return app
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +86,28 @@ class CustomerGroupService extends BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove customer group
|
||||
*
|
||||
* @param {string} groupId id of the customer group to delete
|
||||
* @return {Promise} a promise
|
||||
*/
|
||||
async delete(groupId: string): Promise<void> {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const cgRepo: CustomerGroupRepository = manager.getCustomRepository(
|
||||
this.customerGroupRepository_
|
||||
)
|
||||
|
||||
const customerGroup = await cgRepo.findOne({ where: { id: groupId } })
|
||||
|
||||
if (customerGroup) {
|
||||
await cgRepo.remove(customerGroup)
|
||||
}
|
||||
|
||||
return Promise.resolve()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* List customer groups.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user