Skip to content

Commit 4c9bd0e

Browse files
committed
fix: endpoint multi delete & restore role controller
1 parent 266c626 commit 4c9bd0e

File tree

3 files changed

+234
-5
lines changed

3 files changed

+234
-5
lines changed

docs/swagger/routes/role.js

+124-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,72 @@ module.exports = {
5555
},
5656
},
5757
},
58+
'/role/multiple/delete': {
59+
post: {
60+
tags: ['Role'],
61+
summary: 'Multiple Delete Role',
62+
security: [
63+
{
64+
auth_token: [],
65+
},
66+
],
67+
requestBody: {
68+
required: true,
69+
content: {
70+
'application/x-www-form-urlencoded': {
71+
schema: {
72+
type: 'object',
73+
properties: {
74+
ids: {
75+
type: 'string',
76+
description: '["id_1", "id_2"]',
77+
},
78+
},
79+
required: ['ids'],
80+
},
81+
},
82+
},
83+
},
84+
responses: {
85+
200: {
86+
description: 'Multiple Delete Role',
87+
},
88+
},
89+
},
90+
},
91+
'/role/multiple/restore': {
92+
post: {
93+
tags: ['Role'],
94+
summary: 'Multiple Restore Role',
95+
security: [
96+
{
97+
auth_token: [],
98+
},
99+
],
100+
requestBody: {
101+
required: true,
102+
content: {
103+
'application/x-www-form-urlencoded': {
104+
schema: {
105+
type: 'object',
106+
properties: {
107+
ids: {
108+
type: 'string',
109+
description: '["id_1", "id_2"]',
110+
},
111+
},
112+
required: ['ids'],
113+
},
114+
},
115+
},
116+
},
117+
responses: {
118+
200: {
119+
description: 'Multiple Restore Role',
120+
},
121+
},
122+
},
123+
},
58124
'/role/{id}': {
59125
get: {
60126
tags: ['Role'],
@@ -120,7 +186,63 @@ module.exports = {
120186
},
121187
delete: {
122188
tags: ['Role'],
123-
summary: 'Delete Role By Id',
189+
summary: 'Forever Delete Role By Id',
190+
security: [
191+
{
192+
auth_token: [],
193+
},
194+
],
195+
produces: ['application/json'],
196+
parameters: [
197+
{
198+
in: 'path',
199+
name: 'id',
200+
required: true,
201+
schema: {
202+
type: 'string',
203+
},
204+
description: 'Role Id',
205+
},
206+
],
207+
responses: {
208+
200: {
209+
description: 'Forever Delete Role By Id',
210+
},
211+
},
212+
},
213+
},
214+
'/role/restore/{id}': {
215+
put: {
216+
tags: ['Role'],
217+
summary: 'Restore Role By Id',
218+
security: [
219+
{
220+
auth_token: [],
221+
},
222+
],
223+
produces: ['application/json'],
224+
parameters: [
225+
{
226+
in: 'path',
227+
name: 'id',
228+
required: true,
229+
schema: {
230+
type: 'string',
231+
},
232+
description: 'Role Id',
233+
},
234+
],
235+
responses: {
236+
200: {
237+
description: 'Restore Role By Id',
238+
},
239+
},
240+
},
241+
},
242+
'/role/delete/{id}': {
243+
delete: {
244+
tags: ['Role'],
245+
summary: 'Soft Delete Role By Id',
124246
security: [
125247
{
126248
auth_token: [],
@@ -140,7 +262,7 @@ module.exports = {
140262
],
141263
responses: {
142264
200: {
143-
description: 'Delete Role By Id',
265+
description: 'Soft Delete Role By Id',
144266
},
145267
},
146268
},

src/controllers/Role/controller.ts

+55
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import asyncHandler from 'helpers/asyncHandler'
44
import Authorization from 'middlewares/Authorization'
55
import BuildResponse from 'modules/Response/BuildResponse'
66
import RoleService from 'controllers/Role/service'
7+
import { arrayFormatter } from 'helpers/Common'
78

89
routes.get(
910
'/role',
@@ -54,6 +55,60 @@ routes.put(
5455
})
5556
)
5657

58+
routes.post(
59+
'/role/multiple/delete',
60+
Authorization,
61+
asyncHandler(async function createData(req: Request, res: Response) {
62+
const formData = req.getBody()
63+
const arrayIds = arrayFormatter(formData.ids)
64+
65+
await RoleService.multipleDelete(arrayIds)
66+
const buildResponse = BuildResponse.deleted({})
67+
68+
return res.status(200).json(buildResponse)
69+
})
70+
)
71+
72+
routes.post(
73+
'/role/multiple/restore',
74+
Authorization,
75+
asyncHandler(async function createData(req: Request, res: Response) {
76+
const formData = req.getBody()
77+
const arrayIds = arrayFormatter(formData.ids)
78+
79+
await RoleService.multipleRestore(arrayIds)
80+
const buildResponse = BuildResponse.updated({})
81+
82+
return res.status(200).json(buildResponse)
83+
})
84+
)
85+
86+
routes.delete(
87+
'/role/delete/:id',
88+
Authorization,
89+
asyncHandler(async function deleteData(req: Request, res: Response) {
90+
const { id } = req.getParams()
91+
92+
await RoleService.softDelete(id)
93+
const buildResponse = BuildResponse.deleted({})
94+
95+
return res.status(200).json(buildResponse)
96+
})
97+
)
98+
99+
routes.put(
100+
'/role/restore/:id',
101+
Authorization,
102+
asyncHandler(async function deleteData(req: Request, res: Response) {
103+
const { id } = req.getParams()
104+
105+
await RoleService.restore(id)
106+
const buildResponse = BuildResponse.updated({})
107+
108+
return res.status(200).json(buildResponse)
109+
})
110+
)
111+
57112
routes.delete(
58113
'/role/:id',
59114
Authorization,

src/controllers/Role/service.ts

+55-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { Request } from 'express'
22
import models from 'models'
3+
import db from 'models/_instance'
34
import ResponseError from 'modules/Response/ResponseError'
45
import useValidation from 'helpers/useValidation'
56
import { RoleAttributes } from 'models/role'
67
import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
78
import schema from 'controllers/Role/schema'
89

10+
const { Sequelize } = db
11+
const { Op } = Sequelize
12+
913
const { Role } = models
1014

1115
class RoleService {
@@ -36,8 +40,8 @@ class RoleService {
3640
*
3741
* @param id
3842
*/
39-
public static async getOne(id: string) {
40-
const data = await Role.findByPk(id)
43+
public static async getOne(id: string, paranoid?: boolean) {
44+
const data = await Role.findByPk(id, { paranoid })
4145

4246
if (!data) {
4347
throw new ResponseError.NotFound(
@@ -79,12 +83,60 @@ class RoleService {
7983

8084
/**
8185
*
82-
* @param id
86+
* @param id - Delete Forever
8387
*/
8488
public static async delete(id: string) {
89+
const data = await this.getOne(id)
90+
await data.destroy({ force: true })
91+
}
92+
93+
/**
94+
*
95+
* @param id - Soft Delete
96+
*/
97+
public static async softDelete(id: string) {
8598
const data = await this.getOne(id)
8699
await data.destroy()
87100
}
101+
102+
/**
103+
*
104+
* @param id - Restore data from Trash
105+
*/
106+
public static async restore(id: string) {
107+
const data = await this.getOne(id, false)
108+
await data.restore()
109+
}
110+
111+
/**
112+
*
113+
* @param ids
114+
* @example ["id_1", "id_2"]
115+
*/
116+
public static async multipleDelete(ids: Array<string>) {
117+
await Role.destroy({
118+
where: {
119+
id: {
120+
[Op.in]: ids,
121+
},
122+
},
123+
})
124+
}
125+
126+
/**
127+
*
128+
* @param ids
129+
* @example ["id_1", "id_2"]
130+
*/
131+
public static async multipleRestore(ids: Array<string>) {
132+
await Role.restore({
133+
where: {
134+
id: {
135+
[Op.in]: ids,
136+
},
137+
},
138+
})
139+
}
88140
}
89141

90142
export default RoleService

0 commit comments

Comments
 (0)