-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrequestGenerators.js
112 lines (108 loc) · 3.91 KB
/
requestGenerators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
deleteItem,
fetchItem,
fetchItems,
createItem,
updateItem,
createUser
} from 'api/utils';
import {
apiFailure,
apiSuccess,
createValidatorMiddlewares
} from 'utils/apiUtils';
import { clientCredentialsGrant, managementClient } from 'utils/auth0';
import { REQUEST_TYPES } from './customApisMapper';
import config from 'config';
import { validateObjectId, validateReqBody, validateSchema } from 'utils';
export const generateRequest = (type, router, model, validator) => {
const middlewares = [...createValidatorMiddlewares(validator)];
switch (type) {
case REQUEST_TYPES.create:
middlewares.push(validateSchema(model));
generatePostRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.update:
middlewares.push(validateObjectId, validateReqBody(model));
generatePatchRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.fetchOne:
middlewares.push(validateObjectId);
generateFetchOneRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.fetchAll:
generateFetchAllRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.remove:
middlewares.push(validateObjectId);
generateDeleteRequest({ router, model, middlewares });
break;
}
};
export const generatePostRequest = ({ router, model, middlewares }) => {
router.post('/', ...middlewares, async (req, res) => {
try {
const item = await createItem(model, req.body);
return apiSuccess(res, item);
} catch (err) {
return apiFailure(res, err.message);
}
});
};
export const generatePatchRequest = ({ router, model, middlewares }) => {
router.patch('/:_id', ...middlewares, (req, res, next) => {
const { _id } = req.params;
return updateItem(model, { _id }, req.body)
.then(items => apiSuccess(res, items))
.catch(err => apiFailure(res, err.message));
});
};
export const generateFetchAllRequest = ({
router,
model,
middlewares = []
}) => {
router.get('/', ...middlewares, async (req, res, next) =>
fetchItems(model, req.query)
.then(items => apiSuccess(res, items))
.catch(err => apiFailure(res, err.message))
);
};
export const generateFetchOneRequest = ({ router, model, middlewares }) => {
router.get('/:_id', ...middlewares, async (req, res, next) => {
const { _id } = req.params;
return fetchItem(model, { _id })
.then(item => apiSuccess(res, item))
.catch(err => apiFailure(res, err.message));
});
};
export const generateDeleteRequest = ({ router, model, middlewares }) => {
router.delete('/:_id', ...middlewares, (req, res, next) => {
const { _id } = req.params;
return deleteItem(model, { _id })
.then(items => apiSuccess(res, items))
.catch(err => apiFailure(res, err.message));
});
};
export const generateCreateUserRequest = ({ router, model, validator }) => {
const middlewares = createValidatorMiddlewares(validator);
router.post('/', ...middlewares, async (req, res, next) => {
try {
const { email, password } = req.body;
const auth = await clientCredentialsGrant();
const mgmtAuth0 = await managementClient(auth);
const authUser = await mgmtAuth0.createUser({
connection: config().connection,
email: email,
password: password
});
req.body.authId = authUser.user_id;
let user = await createUser(model, req.body);
user = user._doc;
delete user.authId;
return apiSuccess(res, user);
} catch (err) {
return apiFailure(res, err.message);
}
});
};