@fabrix/spool-cart
Version:
Spool - eCommerce Spool for Fabrix
134 lines (133 loc) • 4.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@fabrix/fabrix/dist/common");
const errors_1 = require("@fabrix/spool-sequelize/dist/errors");
const Validator = require("../../validator");
class FulfillmentController extends common_1.FabrixController {
generalStats(req, res) {
res.json({});
}
findById(req, res) {
const Fulfillment = this.app.models['Fulfillment'];
const id = req.params.id;
Fulfillment.findByIdDefault(id, {})
.then(fulfillment => {
if (!fulfillment) {
throw new errors_1.ModelError('E_NOT_FOUND', `Fulfillment id ${id} not found`);
}
return this.app.services.PermissionsService.sanitizeResult(req, fulfillment);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
count(req, res) {
const EventsService = this.app.services.EventsService;
EventsService.count('Fulfillment')
.then(count => {
const counts = {
fulfillments: count
};
return res.json(counts);
})
.catch(err => {
return res.serverError(err);
});
}
findAll(req, res) {
const orm = this.app.models;
const Fulfillment = orm['Fulfillment'];
const limit = Math.max(0, req.query.limit || 10);
const offset = Math.max(0, req.query.offset || 0);
const sort = req.query.sort || [['created_at', 'DESC']];
const where = req.jsonCriteria(req.query.where);
Fulfillment.findAndCountAll({
order: sort,
offset: offset,
limit: limit,
where: where
})
.then(fulfillments => {
res.paginate(fulfillments.count, limit, offset, sort);
return this.app.services.PermissionsService.sanitizeResult(req, fulfillments.rows);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
create(req, res) {
const FulfillmentService = this.app.services.FulfillmentService;
Validator.validateFulfillment.create(req.body)
.then(values => {
req.body.id = req.params.id;
return FulfillmentService.create(req.body);
})
.then(fulfillment => {
return this.app.services.PermissionsService.sanitizeResult(req, fulfillment);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
update(req, res) {
const FulfillmentService = this.app.services.FulfillmentService;
Validator.validateFulfillment.update(req.body)
.then(values => {
req.body.id = req.params.id;
return FulfillmentService.updateFulfillment(req.body);
})
.then(fulfillment => {
return this.app.services.PermissionsService.sanitizeResult(req, fulfillment);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
cancel(req, res) {
const FulfillmentService = this.app.services.FulfillmentService;
Validator.validateFulfillment.cancel(req.body)
.then(values => {
req.body.id = req.params.id;
return FulfillmentService.cancel(req.body);
})
.then(fulfillment => {
return this.app.services.PermissionsService.sanitizeResult(req, fulfillment);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
destroy(req, res) {
const FulfillmentService = this.app.services.FulfillmentService;
Validator.validateFulfillment.destroy(req.body)
.then(values => {
req.body.id = req.params.id;
return FulfillmentService.destroy(req.body);
})
.then(fulfillment => {
return this.app.services.PermissionsService.sanitizeResult(req, fulfillment);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
}
exports.FulfillmentController = FulfillmentController;