airship-server
Version:
Airship is a framework for Node.JS & TypeScript that helps you to write big, scalable and maintainable API servers.
98 lines • 3.76 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
var SPECIAL_EVENT_ITEM_IDS;
(function (SPECIAL_EVENT_ITEM_IDS) {
SPECIAL_EVENT_ITEM_IDS[SPECIAL_EVENT_ITEM_IDS["ALL"] = -700] = "ALL";
})(SPECIAL_EVENT_ITEM_IDS = exports.SPECIAL_EVENT_ITEM_IDS || (exports.SPECIAL_EVENT_ITEM_IDS = {}));
class NotificationsService {
constructor(db) {
this._db = db;
}
subscribeToNotifications(groupId, userId, eventItemId, subscribe) {
return __awaiter(this, void 0, void 0, function* () {
if (eventItemId === SPECIAL_EVENT_ITEM_IDS.ALL) {
if (subscribe === true) {
yield this._db.NotificationSubscription.destroy({
where: {
groupId,
userId
}
});
yield this._db.NotificationSubscription.create({
groupId,
userId,
eventItemId: SPECIAL_EVENT_ITEM_IDS.ALL
});
}
else {
yield this._db.NotificationSubscription.destroy({
where: {
groupId,
userId
}
});
}
}
let subs = yield this._db.NotificationSubscription.findAll({
where: {
groupId,
userId
}
});
if (subs.length === 1 && subs[0].eventItemId === SPECIAL_EVENT_ITEM_IDS.ALL) {
yield this._db.NotificationSubscription.destroy({
where: {
groupId,
userId
}
});
}
if (subscribe === true) {
let sub = yield this._db.NotificationSubscription.findOne({
where: {
groupId,
userId,
eventItemId
}
});
if (!sub) {
yield this._db.NotificationSubscription.create({
groupId,
userId,
eventItemId
});
}
}
else {
yield this._db.NotificationSubscription.destroy({
where: {
groupId,
userId,
eventItemId
}
});
}
});
}
getSubscribedEventItems(groupId, userId) {
return __awaiter(this, void 0, void 0, function* () {
let subs = yield this._db.NotificationSubscription.findAll({
where: {
groupId,
userId
}
});
return subs.map(s => s.eventItemId);
});
}
}
exports.default = NotificationsService;
//# sourceMappingURL=NotificationsService.js.map