1 | const isArray = require('lodash/isArray');
|
2 |
|
3 | module.exports = ({
|
4 | Ecommerce,
|
5 | router,
|
6 | authMiddleware,
|
7 | permissionMiddleware,
|
8 | asyncMiddleware,
|
9 | getConfig,
|
10 | handleResponse,
|
11 | handleError,
|
12 | }) => {
|
13 |
|
14 | router.get(
|
15 | '/ecommerce/order/message/:message.:ext?',
|
16 | authMiddleware,
|
17 | permissionMiddleware.bind(null, 'ecommerce'),
|
18 | asyncMiddleware(async (req, res) => {
|
19 | const ecommerce = Ecommerce(await getConfig(req.session.slug));
|
20 |
|
21 | try {
|
22 | const order = await ecommerce.getOrder(req.query.orderId);
|
23 | handleResponse(req, res, order.messages[req.params.message].email.html);
|
24 | } catch (error) {
|
25 | handleError(req, res, error);
|
26 | }
|
27 | })
|
28 | );
|
29 |
|
30 | router.get(
|
31 | '/ecommerce/:type.:ext?',
|
32 | authMiddleware,
|
33 | permissionMiddleware.bind(null, 'ecommerce'),
|
34 | asyncMiddleware(async (req, res) => {
|
35 | if (isArray(req.query.sort)) {
|
36 | req.query.sort = JSON.stringify(req.query.sort).replace(/\\"/g, '');
|
37 | }
|
38 |
|
39 | const ecommerce = Ecommerce(await getConfig(req.session.slug));
|
40 |
|
41 | try {
|
42 | handleResponse(req, res, await ecommerce.getType(req.params.type, req.query));
|
43 | } catch (error) {
|
44 | handleError(req, res, error);
|
45 | }
|
46 | })
|
47 | );
|
48 |
|
49 | router.post(
|
50 | '/ecommerce/:type.:ext?',
|
51 | authMiddleware,
|
52 | permissionMiddleware.bind(null, 'ecommerce'),
|
53 | asyncMiddleware(async (req, res) => {
|
54 | if (!/^(discount|order)$/.test(req.params.type)) {
|
55 | handleError(req, res, `Illegal type: ${req.params.type}`);
|
56 | return;
|
57 | }
|
58 |
|
59 | const ecommerce = Ecommerce(await getConfig(req.session.slug));
|
60 |
|
61 | try {
|
62 | handleResponse(req, res, await ecommerce.setType(req.params.type, req.body.item));
|
63 | } catch (error) {
|
64 | handleError(req, res, error);
|
65 | }
|
66 | })
|
67 | );
|
68 |
|
69 | router.delete(
|
70 | '/ecommerce/:type.:ext?',
|
71 | authMiddleware,
|
72 | permissionMiddleware.bind(null, 'ecommerce'),
|
73 | asyncMiddleware(async (req, res) => {
|
74 | if (!/^(discount)$/.test(req.params.type)) {
|
75 | handleError(req, res, `Illegal type: ${req.params.type}`);
|
76 | return;
|
77 | }
|
78 |
|
79 | const ecommerce = Ecommerce(await getConfig(req.session.slug));
|
80 |
|
81 | try {
|
82 | handleResponse(req, res, await ecommerce.deleteType(req.body.item));
|
83 | } catch (error) {
|
84 | handleError(req, res, error);
|
85 | }
|
86 | })
|
87 | );
|
88 |
|
89 | router.get(
|
90 | '/ecommerce/discount/:code.:ext?',
|
91 | asyncMiddleware(async (req, res) => {
|
92 | const ecommerce = Ecommerce(await getConfig(req.session.slug));
|
93 |
|
94 | try {
|
95 | handleResponse(req, res, await ecommerce.verifyDiscount(req.params.code));
|
96 | } catch (error) {
|
97 | handleError(req, res, error);
|
98 | }
|
99 | })
|
100 | );
|
101 | };
|