idea-toolbox
Version:
IDEA's utility functions
138 lines (137 loc) • 5.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExternalCalendarSources = exports.ExternalCalendarToken = exports.ExternalCalendarPermissions = exports.ExternalCalendarInfo = exports.Calendar = void 0;
const resource_model_1 = require("./resource.model");
/**
* Representation of a calendar, which can be:
* - private (linked to a user) or shared (linked to a team).
* - linked to an external service (Microsoft, Google, etc.) or not.
*
* Table: `idea_calendars`.
*
* Indexes:
* - userId-name-index (GSI, all)
* - teamId-name-index (GSI, all)
*/
class Calendar extends resource_model_1.Resource {
load(x) {
super.load(x);
this.calendarId = this.clean(x.calendarId, String);
if (x.teamId)
this.teamId = this.clean(x.teamId, String);
else if (x.userId)
this.userId = this.clean(x.userId, String);
this.name = this.clean(x.name, String);
if (this.name)
this.name = this.name.slice(0, 100);
this.description = this.clean(x.description, String);
if (this.description)
this.description = this.description.slice(0, 300);
this.color = this.clean(x.color, String);
this.timezone = this.clean(x.timezone, String, Intl.DateTimeFormat().resolvedOptions().timeZone);
if (x.external)
this.external = new ExternalCalendarInfo(x.external);
if (x.teamId && x.usersCanManageAppointments)
this.usersCanManageAppointments = this.cleanArray(x.usersCanManageAppointments, String);
if (x.teamId)
this.createdByUserId = this.clean(x.createdByUserId, String);
}
safeLoad(newData, safeData) {
super.safeLoad(newData, safeData);
this.calendarId = safeData.calendarId;
if (safeData.teamId)
this.teamId = safeData.teamId;
else if (safeData.userId)
this.userId = safeData.userId;
if (safeData.external)
this.external = safeData.external;
if (safeData.teamId && safeData.createdByUserId)
this.createdByUserId = safeData.createdByUserId;
}
validate() {
const e = super.validate();
if (this.iE(this.name))
e.push('name');
return e;
}
/**
* Check whether the chosen user can edit the appointments of this calendar.
*/
canUserManageAppointments(userId) {
// if the calendar is linked to external services, the user must have writing access
if (this.external && this.external.userAccess < ExternalCalendarPermissions.WRITER)
return false;
// in case of shared calendar, and the allowance list is set, the user must be in the list of the allowed ones
else if (this.teamId && this.usersCanManageAppointments && !this.usersCanManageAppointments.some(x => x === userId))
return false;
// if no other condition denies it, the user is allowed
else
return true;
}
/**
* Whether the calendar is shared (linked to a team) or not.
*/
isShared() {
return Boolean(this.teamId);
}
/**
* The id to use to represent the calendar, based on the fact the calendar is linked to external sources or not.
*/
getCalendarIdForAppointments() {
return this.external ? this.external.service.concat('_', this.external.calendarId) : this.calendarId;
}
}
exports.Calendar = Calendar;
/**
* Additional info for the calendar, in case it's linked to an external service.
*/
class ExternalCalendarInfo extends resource_model_1.Resource {
load(x) {
super.load(x);
this.service = this.clean(x.service, String);
this.calendarId = this.clean(x.calendarId, String);
this.name = this.clean(x.name, String);
this.lastSyncAt = this.clean(x.lastSyncAt, d => new Date(d).getTime());
this.syncBookmark = this.clean(x.syncBookmark, String);
this.pageBookmark = this.clean(x.pageBookmark, String);
this.userAccess = this.clean(x.userAccess, Number);
this.email = this.clean(x.email, String);
}
}
exports.ExternalCalendarInfo = ExternalCalendarInfo;
/**
* Possible permissions for an external calendar.
*/
var ExternalCalendarPermissions;
(function (ExternalCalendarPermissions) {
ExternalCalendarPermissions[ExternalCalendarPermissions["FREE_BUSY"] = 0] = "FREE_BUSY";
ExternalCalendarPermissions[ExternalCalendarPermissions["READER"] = 1] = "READER";
ExternalCalendarPermissions[ExternalCalendarPermissions["WRITER"] = 2] = "WRITER";
ExternalCalendarPermissions[ExternalCalendarPermissions["OWNER"] = 3] = "OWNER";
})(ExternalCalendarPermissions || (exports.ExternalCalendarPermissions = ExternalCalendarPermissions = {}));
/**
* Additional info on a external calendar, detached from the main object for security reasons.
*
* Table: `idea_externalCalendarsTokens`.
*/
class ExternalCalendarToken extends resource_model_1.Resource {
load(x) {
super.load(x);
this.calendarId = this.clean(x.calendarId, String);
this.token = this.clean(x.token, String);
}
safeLoad(newData, safeData) {
super.safeLoad(newData, safeData);
this.calendarId = safeData.calendarId;
this.token = safeData.token;
}
}
exports.ExternalCalendarToken = ExternalCalendarToken;
/**
* Possible services as source for external calendars.
*/
var ExternalCalendarSources;
(function (ExternalCalendarSources) {
ExternalCalendarSources["GOOGLE"] = "GOOGLE";
ExternalCalendarSources["MICROSOFT"] = "MICROSOFT";
})(ExternalCalendarSources || (exports.ExternalCalendarSources = ExternalCalendarSources = {}));