@fabrix/spool-cart
Version:
Spool - eCommerce Spool for Fabrix
152 lines (151 loc) • 5.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const controllers_1 = require("@fabrix/spool-permissions/dist/api/controllers");
class UserController extends controllers_1.UserController {
customer(req, res) {
const Customer = this.app.models['Customer'];
const customerId = req.params.customer;
let userId = req.params.id;
if (!userId && req.user) {
userId = req.user.id;
}
if (!customerId || !userId || !req.user) {
const err = new Error('A customer id and a customer in session are required');
res.send(401, err);
}
Customer.findById(customerId)
.then(customer => {
return this.app.services.PermissionsService.sanitizeResult(req, customer);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
customers(req, res) {
const Customer = this.app.models['Customer'];
let userId = req.params.id;
if (!userId && req.user) {
userId = req.user.id;
}
if (!userId && !req.user) {
const err = new Error('A user id or a user in session are required');
return res.send(401, err);
}
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']];
Customer.findAndCountAll({
where: {
'$users.id$': userId
},
include: [{
model: this.app.models['User'].instance,
as: 'users',
attributes: ['id']
}],
offset: offset
})
.then(customers => {
res.paginate(customers.count, limit, offset, sort);
return this.app.services.PermissionsService.sanitizeResult(req, customers.rows);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
shop(req, res) {
const Shop = this.app.models['Shop'];
const shopId = req.params.shop;
let userId = req.params.id;
if (!userId && req.user) {
userId = req.user.id;
}
if (!shopId || !userId || !req.user) {
const err = new Error('A shop id and a shop in session are required');
res.send(401, err);
}
Shop.findById(shopId)
.then(shop => {
return this.app.services.PermissionsService.sanitizeResult(req, shop);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
shops(req, res) {
const Shop = this.app.models['Shop'];
let userId = req.params.id;
if (!userId && req.user) {
userId = req.user.id;
}
if (!userId && !req.user) {
const err = new Error('A user id or a user in session are required');
return res.send(401, err);
}
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']];
Shop.findAndCountAll({
where: {
'$users.id$': userId
},
include: [{
model: this.app.models['User'].instance,
as: 'users',
attributes: ['id']
}],
offset: offset
})
.then(shops => {
res.paginate(shops.count, limit, offset, sort);
return this.app.services.PermissionsService.sanitizeResult(req, shops.rows);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
reviews(req, res) {
}
passports(req, res) {
const Passport = this.app.models['Passport'];
const userId = req.params.id;
if (!userId && !req.user) {
const err = new Error('A user id and a user in session are required');
return res.send(401, err);
}
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']];
Passport.findAndCountAll({
user: sort,
where: {
user_id: userId
},
offset: offset,
limit: limit
})
.then(passports => {
res.paginate(passports.count, limit, offset, sort);
return this.app.services.PermissionsService.sanitizeResult(req, passports.rows);
})
.then(result => {
return res.json(result);
})
.catch(err => {
return res.serverError(err);
});
}
}
exports.UserController = UserController;