@wepublish/api
Version:
API core for we.publish.
100 lines • 3.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserConsentService = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const client_1 = require("@prisma/client");
let UserConsentService = exports.UserConsentService = class UserConsentService {
constructor(prisma) {
this.prisma = prisma;
}
userConsentList(filter) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const data = yield this.prisma.userConsent.findMany({
where: {
value: filter === null || filter === void 0 ? void 0 : filter.value,
consent: {
name: filter === null || filter === void 0 ? void 0 : filter.name,
slug: filter === null || filter === void 0 ? void 0 : filter.slug
}
},
orderBy: {
createdAt: 'desc'
},
include: {
user: true,
consent: true
}
});
return data;
});
}
userConsent(id) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const data = yield this.prisma.userConsent.findUnique({
where: {
id
},
include: {
user: true,
consent: true
}
});
if (!data) {
throw new common_1.NotFoundException(`UserConsent with id ${id} not found`);
}
return data;
});
}
createUserConsent(userConsent) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const created = yield this.prisma.userConsent.create({
data: userConsent,
include: { user: true, consent: true }
});
return created;
});
}
updateUserConsent(id, value, user) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const toUpdate = yield this.prisma.userConsent.findFirst({
where: { id },
include: { user: true }
});
// only allow updating for admin or affected user
if (!user.user.roleIDs.includes('admin') && user.user.id !== (toUpdate === null || toUpdate === void 0 ? void 0 : toUpdate.user.id)) {
throw new common_1.ForbiddenException(`Unauthorized`);
}
const updated = yield this.prisma.userConsent.update({
where: { id },
data: {
value
},
include: { user: true, consent: true }
});
return updated;
});
}
deleteUserConsent(id, user) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const toDelete = yield this.prisma.userConsent.findFirst({
where: { id },
include: { user: true }
});
// only allow deleting for admin or affected user
if (!user.user.roleIDs.includes('admin') && user.user.id !== (toDelete === null || toDelete === void 0 ? void 0 : toDelete.user.id)) {
throw new common_1.ForbiddenException(`Unauthorized`);
}
const deleted = yield this.prisma.userConsent.delete({
where: { id },
include: { user: true, consent: true }
});
return deleted;
});
}
};
exports.UserConsentService = UserConsentService = tslib_1.__decorate([
(0, common_1.Injectable)(),
tslib_1.__metadata("design:paramtypes", [client_1.PrismaClient])
], UserConsentService);
//# sourceMappingURL=user-consent.service.js.map