From 8c99d2239947b2f1d349d36539e57d6162a1cbbd Mon Sep 17 00:00:00 2001 From: Pablo Mayobre Date: Thu, 16 Sep 2021 02:08:51 -0300 Subject: [PATCH] Prepare tag release --- dist/helpers/deepClone.d.ts | 1 + dist/helpers/deepClone.js | 8 + dist/helpers/getFulfilledResults.d.ts | 1 + dist/helpers/getFulfilledResults.js | 14 ++ dist/helpers/guaranteeBuffer.d.ts | 2 + dist/helpers/guaranteeBuffer.js | 12 + dist/helpers/guaranteeError.d.ts | 1 + dist/helpers/guaranteeError.js | 9 + dist/helpers/importDirectory.d.ts | 1 + dist/helpers/importDirectory.js | 59 +++++ dist/helpers/index.d.ts | 4 + dist/helpers/index.js | 11 + dist/import/fromNSAC.d.ts | 12 + dist/import/fromNSAC.js | 75 ++++++ dist/import/fromOldData.d.ts | 35 +++ dist/import/fromOldData.js | 137 ++++++++++ dist/import/index.d.ts | 2 + dist/import/index.js | 10 + dist/index.d.ts | 14 ++ dist/index.js | 57 +++++ dist/models/event.d.ts | 0 dist/models/event.js | 1 + dist/models/group.d.ts | 15 ++ dist/models/group.js | 115 +++++++++ dist/models/invite.d.ts | 47 ++++ dist/models/invite.js | 346 ++++++++++++++++++++++++++ dist/models/user.d.ts | 48 ++++ dist/models/user.js | 310 +++++++++++++++++++++++ dist/types/agerange.d.ts | 9 + dist/types/agerange.js | 16 ++ 30 files changed, 1372 insertions(+) create mode 100644 dist/helpers/deepClone.d.ts create mode 100644 dist/helpers/deepClone.js create mode 100644 dist/helpers/getFulfilledResults.d.ts create mode 100644 dist/helpers/getFulfilledResults.js create mode 100644 dist/helpers/guaranteeBuffer.d.ts create mode 100644 dist/helpers/guaranteeBuffer.js create mode 100644 dist/helpers/guaranteeError.d.ts create mode 100644 dist/helpers/guaranteeError.js create mode 100644 dist/helpers/importDirectory.d.ts create mode 100644 dist/helpers/importDirectory.js create mode 100644 dist/helpers/index.d.ts create mode 100644 dist/helpers/index.js create mode 100644 dist/import/fromNSAC.d.ts create mode 100644 dist/import/fromNSAC.js create mode 100644 dist/import/fromOldData.d.ts create mode 100644 dist/import/fromOldData.js create mode 100644 dist/import/index.d.ts create mode 100644 dist/import/index.js create mode 100644 dist/index.d.ts create mode 100644 dist/index.js create mode 100644 dist/models/event.d.ts create mode 100644 dist/models/event.js create mode 100644 dist/models/group.d.ts create mode 100644 dist/models/group.js create mode 100644 dist/models/invite.d.ts create mode 100644 dist/models/invite.js create mode 100644 dist/models/user.d.ts create mode 100644 dist/models/user.js create mode 100644 dist/types/agerange.d.ts create mode 100644 dist/types/agerange.js diff --git a/dist/helpers/deepClone.d.ts b/dist/helpers/deepClone.d.ts new file mode 100644 index 0000000..ff6f16d --- /dev/null +++ b/dist/helpers/deepClone.d.ts @@ -0,0 +1 @@ +export declare const deepClone: (obj: T) => T; diff --git a/dist/helpers/deepClone.js b/dist/helpers/deepClone.js new file mode 100644 index 0000000..5abc1a4 --- /dev/null +++ b/dist/helpers/deepClone.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.deepClone = void 0; +var v8_1 = require("v8"); +var deepClone = function (obj) { + return (0, v8_1.deserialize)((0, v8_1.serialize)(obj)); +}; +exports.deepClone = deepClone; diff --git a/dist/helpers/getFulfilledResults.d.ts b/dist/helpers/getFulfilledResults.d.ts new file mode 100644 index 0000000..fe49425 --- /dev/null +++ b/dist/helpers/getFulfilledResults.d.ts @@ -0,0 +1 @@ +export declare const getFulfilledResults: (results: PromiseSettledResult[]) => T[]; diff --git a/dist/helpers/getFulfilledResults.js b/dist/helpers/getFulfilledResults.js new file mode 100644 index 0000000..6beeb3a --- /dev/null +++ b/dist/helpers/getFulfilledResults.js @@ -0,0 +1,14 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.getFulfilledResults = void 0; +var getFulfilledResults = function (results) { + return results + .map(function (result) { + if (result.status === "fulfilled") { + return result.value; + } + return undefined; + }) + .filter(function (value) { return value !== undefined; }); +}; +exports.getFulfilledResults = getFulfilledResults; diff --git a/dist/helpers/guaranteeBuffer.d.ts b/dist/helpers/guaranteeBuffer.d.ts new file mode 100644 index 0000000..b025649 --- /dev/null +++ b/dist/helpers/guaranteeBuffer.d.ts @@ -0,0 +1,2 @@ +/// +export declare const guaranteeBuffer: (buffer: string | Buffer | ArrayBuffer) => Buffer; diff --git a/dist/helpers/guaranteeBuffer.js b/dist/helpers/guaranteeBuffer.js new file mode 100644 index 0000000..c4a0ac7 --- /dev/null +++ b/dist/helpers/guaranteeBuffer.js @@ -0,0 +1,12 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.guaranteeBuffer = void 0; +var guaranteeBuffer = function (buffer) { + if (buffer instanceof ArrayBuffer) + return Buffer.from(buffer); + if (typeof buffer === "string") { + return Buffer.from(buffer.replace(/^data:image\/[a-z]+;base64,/, ""), "base64"); + } + return buffer; +}; +exports.guaranteeBuffer = guaranteeBuffer; diff --git a/dist/helpers/guaranteeError.d.ts b/dist/helpers/guaranteeError.d.ts new file mode 100644 index 0000000..4449a69 --- /dev/null +++ b/dist/helpers/guaranteeError.d.ts @@ -0,0 +1 @@ +export declare const guaranteeError: (error: unknown) => Error; diff --git a/dist/helpers/guaranteeError.js b/dist/helpers/guaranteeError.js new file mode 100644 index 0000000..6a0a841 --- /dev/null +++ b/dist/helpers/guaranteeError.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.guaranteeError = void 0; +var guaranteeError = function (error) { + return error instanceof Error + ? error + : new Error(typeof error === "string" ? error : "Unknown error"); +}; +exports.guaranteeError = guaranteeError; diff --git a/dist/helpers/importDirectory.d.ts b/dist/helpers/importDirectory.d.ts new file mode 100644 index 0000000..8b942ac --- /dev/null +++ b/dist/helpers/importDirectory.d.ts @@ -0,0 +1 @@ +export declare const importDirectory: (dir: string) => Promise; diff --git a/dist/helpers/importDirectory.js b/dist/helpers/importDirectory.js new file mode 100644 index 0000000..8294519 --- /dev/null +++ b/dist/helpers/importDirectory.js @@ -0,0 +1,59 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + 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) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.importDirectory = void 0; +var path = require("path"); +var importFile = function (file) { return __awaiter(void 0, void 0, void 0, function () { + var _a, name, ext, exports_1; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _a = path.parse(file), name = _a.name, ext = _a.ext; + if (!(name !== "index" && ext === ".ts")) return [3 /*break*/, 2]; + return [4 /*yield*/, Promise.resolve().then(function () { return require("./" + file); })]; + case 1: + exports_1 = _b.sent(); + return [2 /*return*/, [name, exports_1]]; + case 2: return [2 /*return*/]; + } + }); +}); }; +var importDirectory = function (dir) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { + return [2 /*return*/]; +}); }); }; +exports.importDirectory = importDirectory; diff --git a/dist/helpers/index.d.ts b/dist/helpers/index.d.ts new file mode 100644 index 0000000..30da3bb --- /dev/null +++ b/dist/helpers/index.d.ts @@ -0,0 +1,4 @@ +export { deepClone } from "./deepClone"; +export { getFulfilledResults } from "./getFulfilledResults"; +export { guaranteeBuffer } from "./guaranteeBuffer"; +export { guaranteeError } from "./guaranteeError"; diff --git a/dist/helpers/index.js b/dist/helpers/index.js new file mode 100644 index 0000000..ce95cca --- /dev/null +++ b/dist/helpers/index.js @@ -0,0 +1,11 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.guaranteeError = exports.guaranteeBuffer = exports.getFulfilledResults = exports.deepClone = void 0; +var deepClone_1 = require("./deepClone"); +Object.defineProperty(exports, "deepClone", { enumerable: true, get: function () { return deepClone_1.deepClone; } }); +var getFulfilledResults_1 = require("./getFulfilledResults"); +Object.defineProperty(exports, "getFulfilledResults", { enumerable: true, get: function () { return getFulfilledResults_1.getFulfilledResults; } }); +var guaranteeBuffer_1 = require("./guaranteeBuffer"); +Object.defineProperty(exports, "guaranteeBuffer", { enumerable: true, get: function () { return guaranteeBuffer_1.guaranteeBuffer; } }); +var guaranteeError_1 = require("./guaranteeError"); +Object.defineProperty(exports, "guaranteeError", { enumerable: true, get: function () { return guaranteeError_1.guaranteeError; } }); diff --git a/dist/import/fromNSAC.d.ts b/dist/import/fromNSAC.d.ts new file mode 100644 index 0000000..1689ad4 --- /dev/null +++ b/dist/import/fromNSAC.d.ts @@ -0,0 +1,12 @@ +/// +import { AddInvitesOptions, InviteItem } from "../models/invite"; +export declare const parseNSAC: (file: Buffer | ArrayBuffer | string) => Promise; +export declare const importFromNSAC: (file: Buffer | ArrayBuffer | string, event: string, role: string, options: Partial) => Promise<{ + found: import("@typegoose/typegoose").DocumentType[]; + created: import("@typegoose/typegoose").DocumentType[]; + error: { + email: string; + error: Error; + }[]; + deactivated: number; +}>; diff --git a/dist/import/fromNSAC.js b/dist/import/fromNSAC.js new file mode 100644 index 0000000..9d3fbcd --- /dev/null +++ b/dist/import/fromNSAC.js @@ -0,0 +1,75 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + 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) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.importFromNSAC = exports.parseNSAC = void 0; +var xlsx = require("xlsx"); +var guaranteeBuffer_1 = require("../helpers/guaranteeBuffer"); +var invite_1 = require("../models/invite"); +var parseNSAC = function (file) { return __awaiter(void 0, void 0, void 0, function () { + var buffer, workbook, data; + return __generator(this, function (_a) { + buffer = (0, guaranteeBuffer_1.guaranteeBuffer)(file); + if (!buffer) { + throw new Error("File couldn't be converted to a proper buffer"); + } + workbook = xlsx.read(buffer, { type: "buffer" }); + data = xlsx.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]); + return [2 /*return*/, data.map(function (item) { return ({ + email: item.Email, + meta: JSON.stringify({ + location: item.Location, + name: item.Name, + registrationDate: item["Registration Date"], + }), + }); })]; + }); +}); }; +exports.parseNSAC = parseNSAC; +var importFromNSAC = function (file, event, role, options) { return __awaiter(void 0, void 0, void 0, function () { + var invites; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, (0, exports.parseNSAC)(file)]; + case 1: + invites = _a.sent(); + return [4 /*yield*/, invite_1.InviteModel.addInviteList(event, invites, role, options)]; + case 2: return [2 /*return*/, _a.sent()]; + } + }); +}); }; +exports.importFromNSAC = importFromNSAC; diff --git a/dist/import/fromOldData.d.ts b/dist/import/fromOldData.d.ts new file mode 100644 index 0000000..0770ad6 --- /dev/null +++ b/dist/import/fromOldData.d.ts @@ -0,0 +1,35 @@ +/// +import { InviteItem } from "../models/invite"; +import { UserItem } from "../models/user"; +export declare const parseOldData: (file: Buffer | ArrayBuffer | string) => Promise<[UserItem[], InviteItem[]]>; +declare type Overwrites = { + role: boolean; + inviteMeta: boolean; + userMeta: boolean; + name: boolean; +}; +export declare const importFromParsedOldData: (users: UserItem[], invites: InviteItem[], event?: string, overwrites?: Partial) => Promise<{ + users: import("@typegoose/typegoose").DocumentType[]; + invites: { + found: import("@typegoose/typegoose").DocumentType[]; + created: import("@typegoose/typegoose").DocumentType[]; + error: { + email: string; + error: Error; + }[]; + deactivated: number; + }; +}>; +export declare const importFromOldData: (file: Buffer | ArrayBuffer | string, event?: string, overwrites?: Partial) => Promise<{ + users: import("@typegoose/typegoose").DocumentType[]; + invites: { + found: import("@typegoose/typegoose").DocumentType[]; + created: import("@typegoose/typegoose").DocumentType[]; + error: { + email: string; + error: Error; + }[]; + deactivated: number; + }; +}>; +export {}; diff --git a/dist/import/fromOldData.js b/dist/import/fromOldData.js new file mode 100644 index 0000000..2dc1f80 --- /dev/null +++ b/dist/import/fromOldData.js @@ -0,0 +1,137 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + 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) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.importFromOldData = exports.importFromParsedOldData = exports.parseOldData = void 0; +var xlsx = require("xlsx"); +var guaranteeBuffer_1 = require("../helpers/guaranteeBuffer"); +var getFulfilledResults_1 = require("../helpers/getFulfilledResults"); +var invite_1 = require("../models/invite"); +var user_1 = require("../models/user"); +var parseOldData = function (file) { return __awaiter(void 0, void 0, void 0, function () { + var buffer, workbook, data, invites, users; + return __generator(this, function (_a) { + buffer = (0, guaranteeBuffer_1.guaranteeBuffer)(file); + if (!buffer) { + throw new Error("File couldn't be converted to a proper buffer"); + } + workbook = xlsx.read(buffer, { type: "buffer", cellDates: true }); + data = xlsx.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]); + invites = data.map(function (item) { return ({ + email: item.mail, + role: item.role, + meta: JSON.stringify({ + location: item.location, + name: item.name, + }), + }); }); + users = data + .filter(function (item) { return Boolean(item.memberid); }) + .map(function (item) { + var _a; + return ({ + id: item.memberid, + displayName: item.name, + invite: item.mail, + meta: item.birthday + ? JSON.stringify({ possibleBirthday: (_a = item.birthday) === null || _a === void 0 ? void 0 : _a.toISOString() }) + : undefined, + }); + }); + return [2 /*return*/, [users, invites]]; + }); +}); }; +exports.parseOldData = parseOldData; +var importFromParsedOldData = function (users, invites, event, overwrites) { + if (event === void 0) { event = "nsac2020"; } + if (overwrites === void 0) { overwrites = {}; } + return __awaiter(void 0, void 0, void 0, function () { + var invitesResult, usersResult; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + overwrites = __assign({ role: true, inviteMeta: true, userMeta: true, name: true }, overwrites); + return [4 /*yield*/, invite_1.InviteModel.addInviteList(event, invites, "participant", { + addRole: !overwrites.role, + overwriteMeta: !overwrites.inviteMeta, + deactivateMissing: false, + })]; + case 1: + invitesResult = _a.sent(); + return [4 /*yield*/, user_1.UserModel.addUserList(event, users, { + overwriteMeta: overwrites.userMeta, + overwriteName: overwrites.name + })]; + case 2: + usersResult = _a.sent(); + return [2 /*return*/, { + users: (0, getFulfilledResults_1.getFulfilledResults)(usersResult), + invites: invitesResult, + }]; + } + }); + }); +}; +exports.importFromParsedOldData = importFromParsedOldData; +var importFromOldData = function (file, event, overwrites) { + if (event === void 0) { event = "nsac2020"; } + if (overwrites === void 0) { overwrites = {}; } + return __awaiter(void 0, void 0, void 0, function () { + var _a, users, invites; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, (0, exports.parseOldData)(file)]; + case 1: + _a = _b.sent(), users = _a[0], invites = _a[1]; + return [4 /*yield*/, (0, exports.importFromParsedOldData)(users, invites, event, overwrites)]; + case 2: return [2 /*return*/, _b.sent()]; + } + }); + }); +}; +exports.importFromOldData = importFromOldData; diff --git a/dist/import/index.d.ts b/dist/import/index.d.ts new file mode 100644 index 0000000..f7d10c9 --- /dev/null +++ b/dist/import/index.d.ts @@ -0,0 +1,2 @@ +export { importFromOldData, parseOldData, importFromParsedOldData as importOldDataItems } from "./fromOldData"; +export { importFromNSAC, parseNSAC } from "./fromNSAC"; diff --git a/dist/import/index.js b/dist/import/index.js new file mode 100644 index 0000000..5eb4e46 --- /dev/null +++ b/dist/import/index.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.parseNSAC = exports.importFromNSAC = exports.importOldDataItems = exports.parseOldData = exports.importFromOldData = void 0; +var fromOldData_1 = require("./fromOldData"); +Object.defineProperty(exports, "importFromOldData", { enumerable: true, get: function () { return fromOldData_1.importFromOldData; } }); +Object.defineProperty(exports, "parseOldData", { enumerable: true, get: function () { return fromOldData_1.parseOldData; } }); +Object.defineProperty(exports, "importOldDataItems", { enumerable: true, get: function () { return fromOldData_1.importFromParsedOldData; } }); +var fromNSAC_1 = require("./fromNSAC"); +Object.defineProperty(exports, "importFromNSAC", { enumerable: true, get: function () { return fromNSAC_1.importFromNSAC; } }); +Object.defineProperty(exports, "parseNSAC", { enumerable: true, get: function () { return fromNSAC_1.parseNSAC; } }); diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..645da83 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,14 @@ +import * as mongoose from "mongoose"; +import { ConnectOptions as MongooseConnectOptions } from "mongoose"; +export * as helpers from "./helpers"; +export * as importers from "./import"; +export * from "./models/user"; +export * from "./models/invite"; +export * from "./models/group"; +export { AgeRange } from "./types/agerange"; +export declare type ConnectOptions = MongooseConnectOptions & { + host?: string; + port?: string; +}; +export declare const Database: typeof mongoose; +export declare const connect: ({ host, port, ...options }?: ConnectOptions) => Promise; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..744676d --- /dev/null +++ b/dist/index.js @@ -0,0 +1,57 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.connect = exports.Database = exports.AgeRange = exports.importers = exports.helpers = void 0; +var mongoose = require("mongoose"); +exports.helpers = require("./helpers"); +exports.importers = require("./import"); +__exportStar(require("./models/user"), exports); +__exportStar(require("./models/invite"), exports); +__exportStar(require("./models/group"), exports); +var agerange_1 = require("./types/agerange"); +Object.defineProperty(exports, "AgeRange", { enumerable: true, get: function () { return agerange_1.AgeRange; } }); +exports.Database = mongoose; +var connect = function (_a) { + var _b, _c; + if (_a === void 0) { _a = {}; } + var host = _a.host, port = _a.port, options = __rest(_a, ["host", "port"]); + host !== null && host !== void 0 ? host : (host = process.env.MONGODB_HOST); + port !== null && port !== void 0 ? port : (port = process.env.MONGODB_PORT); + if (!host) + throw new Error("A host was not provided"); + var finalPort = port && port.trim() !== "" ? ":" + port.trim() : ""; + console.log(process.env.MONGODB_USERNAME, process.env.MONGODB_PASSWORD); + return mongoose.connect("mongodb://" + host.trim() + finalPort + "/", __assign({ dbName: "tmsa", authSource: "tmsa", user: (_b = process.env.MONGODB_USERNAME) === null || _b === void 0 ? void 0 : _b.trim(), pass: (_c = process.env.MONGODB_PASSWORD) === null || _c === void 0 ? void 0 : _c.trim(), useNewUrlParser: true, useUnifiedTopology: true }, options)); +}; +exports.connect = connect; diff --git a/dist/models/event.d.ts b/dist/models/event.d.ts new file mode 100644 index 0000000..e69de29 diff --git a/dist/models/event.js b/dist/models/event.js new file mode 100644 index 0000000..3918c74 --- /dev/null +++ b/dist/models/event.js @@ -0,0 +1 @@ +"use strict"; diff --git a/dist/models/group.d.ts b/dist/models/group.d.ts new file mode 100644 index 0000000..a162c39 --- /dev/null +++ b/dist/models/group.d.ts @@ -0,0 +1,15 @@ +import { Ref, DocumentType, ReturnModelType } from "@typegoose/typegoose"; +import { Types } from "mongoose"; +import { Invite } from "./invite"; +export declare class Group { + event: string; + members: Ref[]; + channels: string[]; + meta?: string; + getInviteIndex(this: DocumentType, invite: Ref): number; + hasInvite(this: DocumentType, invite: Ref): boolean; + addInvite(this: DocumentType, invite: Ref): void; + removeInvite(this: DocumentType, invite: Ref): boolean; + static fetchGroup(this: ReturnModelType, group: Ref): Promise | null>; +} +export declare const GroupModel: ReturnModelType; diff --git a/dist/models/group.js b/dist/models/group.js new file mode 100644 index 0000000..55e847c --- /dev/null +++ b/dist/models/group.js @@ -0,0 +1,115 @@ +"use strict"; +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + 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) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.GroupModel = exports.Group = void 0; +var typegoose_1 = require("@typegoose/typegoose"); +var invite_1 = require("./invite"); +var Group = /** @class */ (function () { + function Group() { + } + Group.prototype.getInviteIndex = function (invite) { + if (!invite) + return -1; + if (this.members.length === 0) + return -1; + var id = invite_1.InviteModel.getId(invite); + if (!id) + return -1; + return this.members.findIndex(function (found) { + return invite_1.InviteModel.getId(found) === id; + }); + }; + Group.prototype.hasInvite = function (invite) { + return this.getInviteIndex(invite) !== -1; + }; + Group.prototype.addInvite = function (invite) { + if (this.hasInvite(invite)) + return; + this.members.push(invite); + }; + Group.prototype.removeInvite = function (invite) { + var index = this.getInviteIndex(invite); + if (index === -1) + return false; + this.members.splice(index, 1); + return true; + }; + Group.fetchGroup = function (group) { + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!group) + return [2 /*return*/, null]; + if ((0, typegoose_1.isDocument)(group)) + return [2 /*return*/, group]; + return [4 /*yield*/, this.findOne({ _id: group })]; + case 1: return [2 /*return*/, _a.sent()]; + } + }); + }); + }; + __decorate([ + (0, typegoose_1.prop)({ required: true }), + __metadata("design:type", String) + ], Group.prototype, "event", void 0); + __decorate([ + (0, typegoose_1.prop)({ ref: function () { return "Invite"; }, required: true, default: function () { return []; } }), + __metadata("design:type", Array) + ], Group.prototype, "members", void 0); + __decorate([ + (0, typegoose_1.prop)({ type: String, required: true, default: function () { return []; } }), + __metadata("design:type", Array) + ], Group.prototype, "channels", void 0); + __decorate([ + (0, typegoose_1.prop)(), + __metadata("design:type", String) + ], Group.prototype, "meta", void 0); + return Group; +}()); +exports.Group = Group; +exports.GroupModel = (0, typegoose_1.getModelForClass)(Group); diff --git a/dist/models/invite.d.ts b/dist/models/invite.d.ts new file mode 100644 index 0000000..bc94495 --- /dev/null +++ b/dist/models/invite.d.ts @@ -0,0 +1,47 @@ +import { Ref, DocumentType, ReturnModelType } from "@typegoose/typegoose"; +import { Types } from "mongoose"; +import { User } from "./user"; +import { Group } from "./group"; +export declare type RevokeInviteOptions = { + revokeFromUser?: boolean; + returnUser?: boolean; +}; +export declare type AddInvitesOptions = { + addRole: boolean; + deactivateMissing: boolean; + overwriteMeta: boolean; +}; +export declare type InviteItem = { + email: string; + meta?: string; + role?: string; +}; +export declare class Invite { + active: boolean; + event: string; + email: string; + role: string[]; + meta?: string; + user?: Ref; + group?: Ref; + assignToGroup(this: DocumentType, group: Ref): Promise; + revoke(this: DocumentType, options?: RevokeInviteOptions): Promise | null>; + resolve(this: DocumentType, user: DocumentType, forceRevoke?: boolean): Promise; + isEqual(this: DocumentType, invite: Ref): boolean; + setRole(this: DocumentType, role: string, add?: boolean): Promise; + static getId(this: ReturnModelType, invite: Ref): Types.ObjectId | undefined; + static findExact(this: ReturnModelType, event: string, email: string): import("mongoose").QueryWithHelpers | null, DocumentType, import("@typegoose/typegoose/lib/types").BeAnObject, DocumentType>; + static addInvite(this: ReturnModelType, event: string, email: string): Promise<[DocumentType, boolean]>; + static addInviteList(this: ReturnModelType, event: string, invites: InviteItem[], role: string, options?: Partial): Promise<{ + found: DocumentType[]; + created: DocumentType[]; + error: { + email: string; + error: Error; + }[]; + deactivated: number; + }>; + static fetchInvite(this: ReturnModelType, invite: Ref): Promise | undefined>; + static fetchAllInvites(this: ReturnModelType, invites: Ref[]): Promise[]>; +} +export declare const InviteModel: ReturnModelType; diff --git a/dist/models/invite.js b/dist/models/invite.js new file mode 100644 index 0000000..4ef68f8 --- /dev/null +++ b/dist/models/invite.js @@ -0,0 +1,346 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + 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) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.InviteModel = exports.Invite = void 0; +var typegoose_1 = require("@typegoose/typegoose"); +var user_1 = require("./user"); +var group_1 = require("./group"); +var guaranteeError_1 = require("../helpers/guaranteeError"); +var getFulfilledResults_1 = require("../helpers/getFulfilledResults"); +var Invite = /** @class */ (function () { + function Invite() { + } + Invite_1 = Invite; + Invite.prototype.assignToGroup = function (group) { + return __awaiter(this, void 0, void 0, function () { + var fullGroup, fullGroup; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!this.group) return [3 /*break*/, 2]; + return [4 /*yield*/, group_1.GroupModel.fetchGroup(this.group)]; + case 1: + fullGroup = _a.sent(); + if (fullGroup) + fullGroup.removeInvite(this); + _a.label = 2; + case 2: + if (!group) return [3 /*break*/, 4]; + return [4 /*yield*/, group_1.GroupModel.fetchGroup(group)]; + case 3: + fullGroup = _a.sent(); + if (!fullGroup) + return [2 /*return*/, false]; + fullGroup.addInvite(this); + _a.label = 4; + case 4: + this.group = group; + return [2 /*return*/, true]; + } + }); + }); + }; + Invite.prototype.revoke = function (options) { + if (options === void 0) { options = { revokeFromUser: false, returnUser: false }; } + return __awaiter(this, void 0, void 0, function () { + var _a, revokeFromUser, _b, returnUser, user, revoked; + return __generator(this, function (_c) { + switch (_c.label) { + case 0: + _a = options.revokeFromUser, revokeFromUser = _a === void 0 ? false : _a, _b = options.returnUser, returnUser = _b === void 0 ? false : _b; + if (!this.user) + return [2 /*return*/, null]; + if (!(returnUser || revokeFromUser)) return [3 /*break*/, 2]; + return [4 /*yield*/, user_1.UserModel.fetchUser(this.user)]; + case 1: + user = _c.sent(); + _c.label = 2; + case 2: + this.user = undefined; + if (!user) + return [2 /*return*/, null]; + if (!revokeFromUser) return [3 /*break*/, 4]; + return [4 /*yield*/, user.revokeInvite(this.event)]; + case 3: + revoked = _c.sent(); + if (this._id !== (revoked === null || revoked === void 0 ? void 0 : revoked._id)) { + console.error("Probably revoked an erroneous invite\n - The user was: " + user.toString() + "\n - The revoked that got revoked was: " + (revoked === null || revoked === void 0 ? void 0 : revoked.toString()) + "\n - The invite that had to be revoked was: " + this.toString() + "}\n "); + } + _c.label = 4; + case 4: return [2 /*return*/, returnUser ? user : null]; + } + }); + }); + }; + Invite.prototype.resolve = function (user, forceRevoke) { + if (forceRevoke === void 0) { forceRevoke = false; } + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + if (this.user && forceRevoke) { + this.revoke({ revokeFromUser: true }); + } + this.user = user; + return [2 /*return*/]; + }); + }); + }; + Invite.prototype.isEqual = function (invite) { + if ((0, typegoose_1.isDocument)(invite)) + return invite === this || invite._id === this._id; + return invite && this._id === invite; + }; + Invite.prototype.setRole = function (role, add) { + if (add === void 0) { add = false; } + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + if (add) { + if (!this.role.includes(role)) + this.role.push(role); + } + else { + this.role.splice(0, this.role.length); + this.role.push("role"); + } + return [2 /*return*/]; + }); + }); + }; + Invite.getId = function (invite) { + if (!invite) + return undefined; + if ((0, typegoose_1.isDocument)(invite)) + return invite._id; + if (invite instanceof Invite_1) + return invite._id; + return invite; + }; + Invite.findExact = function (event, email) { + return this.findOne({ event: event, email: email }); + }; + Invite.addInvite = function (event, email) { + return __awaiter(this, void 0, void 0, function () { + var found, created, user; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, this.findExact(event, email)]; + case 1: + found = _a.sent(); + if (found) + return [2 /*return*/, [found, true]]; + return [4 /*yield*/, this.create({ event: event, email: email })]; + case 2: + created = _a.sent(); + return [4 /*yield*/, user_1.UserModel.findByUnresolvedInvite(event, email)]; + case 3: + user = _a.sent(); + if (!user) return [3 /*break*/, 5]; + user.forcedResolveInvite(created); + return [4 /*yield*/, user.save()]; + case 4: + _a.sent(); + _a.label = 5; + case 5: return [2 /*return*/, [created, false]]; + } + }); + }); + }; + Invite.addInviteList = function (event, invites, role, options) { + if (options === void 0) { options = {}; } + return __awaiter(this, void 0, void 0, function () { + var opts, result, updated; + var _this = this; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + opts = __assign({ addRole: false, deactivateMissing: false, overwriteMeta: true }, options); + result = { + found: [], + created: [], + error: [], + deactivated: 0, + }; + if (!opts.deactivateMissing) return [3 /*break*/, 2]; + return [4 /*yield*/, this.updateMany({ role: role, active: true }, { active: false })]; + case 1: + updated = _a.sent(); + result.deactivated = updated.nModified; + _a.label = 2; + case 2: return [4 /*yield*/, Promise.allSettled(invites.map(function (_a) { var _b, _c; return __awaiter(_this, void 0, void 0, function () { + var _d, invite, found, error_1; + var email = _a.email, item = __rest(_a, ["email"]); + return __generator(this, function (_e) { + switch (_e.label) { + case 0: + _e.trys.push([0, 3, , 4]); + return [4 /*yield*/, this.addInvite(event, email)]; + case 1: + _d = _e.sent(), invite = _d[0], found = _d[1]; + if (found) { + result.found.push(invite); + } + else { + result.created.push(invite); + } + invite.active = true; + invite.meta = opts.overwriteMeta + ? item.meta + : (_b = invite.meta) !== null && _b !== void 0 ? _b : item.meta; + invite.setRole((_c = item.role) !== null && _c !== void 0 ? _c : role, opts.addRole); + return [4 /*yield*/, invite.save()]; + case 2: + _e.sent(); + return [3 /*break*/, 4]; + case 3: + error_1 = _e.sent(); + result.error.push({ email: email, error: (0, guaranteeError_1.guaranteeError)(error_1) }); + return [3 /*break*/, 4]; + case 4: return [2 /*return*/, true]; + } + }); + }); }))]; + case 3: + _a.sent(); + if (opts.deactivateMissing) { + result.deactivated -= result.found.length; + } + return [2 /*return*/, result]; + } + }); + }); + }; + Invite.fetchInvite = function (invite) { + var _a; + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + if (!invite) + return [2 /*return*/, undefined]; + if ((0, typegoose_1.isDocument)(invite)) + return [2 /*return*/, invite]; + return [4 /*yield*/, this.findOne({ _id: invite })]; + case 1: return [2 /*return*/, (_a = (_b.sent())) !== null && _a !== void 0 ? _a : undefined]; + } + }); + }); + }; + Invite.fetchAllInvites = function (invites) { + return __awaiter(this, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _a = getFulfilledResults_1.getFulfilledResults; + return [4 /*yield*/, Promise.allSettled(invites.map(this.fetchInvite))]; + case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; + } + }); + }); + }; + var Invite_1; + __decorate([ + (0, typegoose_1.prop)({ required: true, default: true }), + __metadata("design:type", Boolean) + ], Invite.prototype, "active", void 0); + __decorate([ + (0, typegoose_1.prop)({ required: true }), + __metadata("design:type", String) + ], Invite.prototype, "event", void 0); + __decorate([ + (0, typegoose_1.prop)({ required: true }), + __metadata("design:type", String) + ], Invite.prototype, "email", void 0); + __decorate([ + (0, typegoose_1.prop)({ + type: String, + required: true, + default: function () { return ["participant"]; }, + minlength: 1, + }), + __metadata("design:type", Array) + ], Invite.prototype, "role", void 0); + __decorate([ + (0, typegoose_1.prop)(), + __metadata("design:type", String) + ], Invite.prototype, "meta", void 0); + __decorate([ + (0, typegoose_1.prop)({ ref: function () { return "User"; } }), + __metadata("design:type", Object) + ], Invite.prototype, "user", void 0); + __decorate([ + (0, typegoose_1.prop)({ ref: function () { return "Group"; } }), + __metadata("design:type", Object) + ], Invite.prototype, "group", void 0); + Invite = Invite_1 = __decorate([ + (0, typegoose_1.index)({ event: 1, email: 1 }, { unique: true }) + ], Invite); + return Invite; +}()); +exports.Invite = Invite; +exports.InviteModel = (0, typegoose_1.getModelForClass)(Invite); diff --git a/dist/models/user.d.ts b/dist/models/user.d.ts new file mode 100644 index 0000000..f331960 --- /dev/null +++ b/dist/models/user.d.ts @@ -0,0 +1,48 @@ +import { Ref, DocumentType, ReturnModelType } from "@typegoose/typegoose"; +import { Types } from "mongoose"; +import { AgeRange } from "../types/agerange"; +import { Invite } from "./invite"; +export declare type AddUsersOptions = { + overwriteName: boolean; + overwriteMeta: boolean; +}; +export declare type UserItem = { + id: string; + displayName: string; + invite: string; + meta?: string; +}; +export declare class User { + discordId: string; + name: string; + inDiscord: boolean; + ageRange?: AgeRange; + roles: string[]; + unresolvedInvites: Map; + meta?: string; + resolvedInvites: Ref[]; + isValidated(this: DocumentType): Promise; + revokeInvite(this: DocumentType, event: string): Promise | null>; + forcedResolveInvite(this: DocumentType, invite: DocumentType): Promise; + resolveInvite(this: DocumentType, event: string, forceRevoke?: boolean): Promise | null>; + setInvite(this: DocumentType, event: string, email?: string): Promise | null, DocumentType | null] | undefined>; + getInvite(this: DocumentType, event: string, unresolved?: boolean): Promise; + getInvite(this: DocumentType, event: string, unresolved?: false): Promise; + getInvite(this: DocumentType, event: string, unresolved: true): Promise; + static createFromDiscord(this: ReturnModelType, user: { + id: string; + displayName: string; + }): Promise>; + static findFromDiscord(this: ReturnModelType, user: { + id: string; + displayName: string; + }): import("mongoose").QueryWithHelpers | null, DocumentType, import("@typegoose/typegoose/lib/types").BeAnObject, DocumentType>; + static addFromDiscord(this: ReturnModelType, user: { + id: string; + displayName: string; + }): Promise, boolean]>; + static addUserList(event: string, users: UserItem[], options?: Partial): Promise>[]>; + static fetchUser(this: ReturnModelType, user: Ref): Promise | null>; + static findByUnresolvedInvite(this: ReturnModelType, event: string, email: string): Promise | null>; +} +export declare const UserModel: ReturnModelType; diff --git a/dist/models/user.js b/dist/models/user.js new file mode 100644 index 0000000..8b1c008 --- /dev/null +++ b/dist/models/user.js @@ -0,0 +1,310 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + 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) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.UserModel = exports.User = void 0; +var typegoose_1 = require("@typegoose/typegoose"); +var agerange_1 = require("../types/agerange"); +var invite_1 = require("./invite"); +var User = /** @class */ (function () { + function User() { + } + User_1 = User; + User.prototype.isValidated = function () { + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + return [2 /*return*/, this.ageRange && this.ageRange !== agerange_1.AgeRange.unspecified]; + }); + }); + }; + User.prototype.revokeInvite = function (event) { + return __awaiter(this, void 0, void 0, function () { + var invites, resolved, revoked; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, invite_1.InviteModel.fetchAllInvites(this.resolvedInvites)]; + case 1: + invites = _a.sent(); + resolved = invites.find(function (invite) { return invite.event === event; }); + if (!resolved) return [3 /*break*/, 3]; + return [4 /*yield*/, resolved.revoke({ returnUser: true })]; + case 2: + revoked = _a.sent(); + if ((revoked === null || revoked === void 0 ? void 0 : revoked._id) !== this._id) { + console.error("Probably revoked an erroneous invite\n- The invite was: " + resolved.toString() + "\n- The user that got revoked was: " + (revoked === null || revoked === void 0 ? void 0 : revoked.toString()) + "\n- The user that had to be revoked was: " + this.toString() + "}\n"); + } + this.resolvedInvites.splice(this.resolvedInvites.findIndex(function (invite) { return resolved.isEqual(invite); }), 1); + return [2 /*return*/, resolved]; + case 3: + this.unresolvedInvites.delete(event); + return [2 /*return*/, null]; + } + }); + }); + }; + User.prototype.forcedResolveInvite = function (invite) { + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + this.unresolvedInvites.delete(invite.event); + this.resolvedInvites.push(invite); + return [2 /*return*/]; + }); + }); + }; + User.prototype.resolveInvite = function (event, forceRevoke) { + if (forceRevoke === void 0) { forceRevoke = false; } + return __awaiter(this, void 0, void 0, function () { + var email, invite, resolved; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + email = this.unresolvedInvites.get(event); + if (!email) + return [2 /*return*/, null]; + return [4 /*yield*/, invite_1.InviteModel.findExact(event, email)]; + case 1: + invite = _a.sent(); + if (!invite) + return [2 /*return*/, null]; + resolved = invite.resolve(this, forceRevoke); + if (!resolved) + return [2 /*return*/, null]; + this.forcedResolveInvite(invite); + return [2 /*return*/, invite]; + } + }); + }); + }; + User.prototype.setInvite = function (event, email) { + return __awaiter(this, void 0, void 0, function () { + var revoked, resolved; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, this.revokeInvite(event)]; + case 1: + revoked = _a.sent(); + if (!(email && email !== "")) return [3 /*break*/, 3]; + this.unresolvedInvites.set(event, email); + return [4 /*yield*/, this.resolveInvite(event)]; + case 2: + resolved = _a.sent(); + return [2 /*return*/, [revoked, resolved]]; + case 3: return [2 /*return*/]; + } + }); + }); + }; + User.prototype.getInvite = function (event, unresolved) { + var _a; + if (unresolved === void 0) { unresolved = false; } + return __awaiter(this, void 0, void 0, function () { + var found; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + if (!unresolved) return [3 /*break*/, 1]; + return [2 /*return*/, (_a = this.unresolvedInvites.get(event)) !== null && _a !== void 0 ? _a : null]; + case 1: return [4 /*yield*/, invite_1.InviteModel.fetchAllInvites(this.resolvedInvites)]; + case 2: + found = (_b.sent()).find(function (invite) { return invite.event === event; }); + return [2 /*return*/, found ? found : null]; + } + }); + }); + }; + User.createFromDiscord = function (user) { + return this.create({ + discordId: user.id, + name: user.displayName, + inDiscord: true, + }); + }; + User.findFromDiscord = function (user) { + return this.findOne({ discordId: user.id }); + }; + User.addFromDiscord = function (user) { + return __awaiter(this, void 0, void 0, function () { + var found, _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, this.findFromDiscord(user)]; + case 1: + found = _b.sent(); + if (!(found !== null && found !== void 0)) return [3 /*break*/, 2]; + _a = found; + return [3 /*break*/, 4]; + case 2: return [4 /*yield*/, this.createFromDiscord(user)]; + case 3: + _a = (_b.sent()); + _b.label = 4; + case 4: return [2 /*return*/, [_a, !!found]]; + } + }); + }); + }; + User.addUserList = function (event, users, options) { + if (options === void 0) { options = {}; } + return __awaiter(this, void 0, void 0, function () { + var opts; + var _this = this; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + opts = __assign({ overwriteName: true, overwriteMeta: true }, options); + return [4 /*yield*/, Promise.allSettled(users.map(function (info) { return __awaiter(_this, void 0, void 0, function () { + var user; + var _a, _b; + return __generator(this, function (_c) { + switch (_c.label) { + case 0: return [4 /*yield*/, exports.UserModel.addFromDiscord(info)]; + case 1: + user = (_c.sent())[0]; + user.name = opts.overwriteName ? info.displayName : user.name; + user.meta = opts.overwriteMeta + ? (_a = user.meta) !== null && _a !== void 0 ? _a : info.meta + : (_b = info.meta) !== null && _b !== void 0 ? _b : user.meta; + return [4 /*yield*/, user.setInvite(event, info.invite)]; + case 2: + _c.sent(); + return [4 /*yield*/, user.save()]; + case 3: return [2 /*return*/, _c.sent()]; + } + }); + }); }))]; + case 1: return [2 /*return*/, _a.sent()]; + } + }); + }); + }; + User.fetchUser = function (user) { + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!user) + return [2 /*return*/, null]; + if ((0, typegoose_1.isDocument)(user)) + return [2 /*return*/, user]; + return [4 /*yield*/, this.findOne({ _id: user })]; + case 1: return [2 /*return*/, _a.sent()]; + } + }); + }); + }; + User.findByUnresolvedInvite = function (event, email) { + var _a; + return this.findOne((_a = {}, + _a["unresolvedInvites." + event] = email, + _a)).exec(); + }; + var User_1; + __decorate([ + (0, typegoose_1.prop)({ required: true, unique: true }), + __metadata("design:type", String) + ], User.prototype, "discordId", void 0); + __decorate([ + (0, typegoose_1.prop)({ required: true }), + __metadata("design:type", String) + ], User.prototype, "name", void 0); + __decorate([ + (0, typegoose_1.prop)({ required: true, default: false }), + __metadata("design:type", Boolean) + ], User.prototype, "inDiscord", void 0); + __decorate([ + (0, typegoose_1.prop)({ enum: agerange_1.AgeRange, type: Number, default: agerange_1.AgeRange.unspecified }), + __metadata("design:type", Number) + ], User.prototype, "ageRange", void 0); + __decorate([ + (0, typegoose_1.prop)({ type: String, required: true, default: function () { return []; } }), + __metadata("design:type", Array) + ], User.prototype, "roles", void 0); + __decorate([ + (0, typegoose_1.prop)({ + type: String, + required: true, + default: function () { return new Map(); }, + }), + __metadata("design:type", Map) + ], User.prototype, "unresolvedInvites", void 0); + __decorate([ + (0, typegoose_1.prop)(), + __metadata("design:type", String) + ], User.prototype, "meta", void 0); + __decorate([ + (0, typegoose_1.prop)({ ref: function () { return "Invite"; }, required: true, default: function () { return []; } }), + __metadata("design:type", Array) + ], User.prototype, "resolvedInvites", void 0); + User = User_1 = __decorate([ + (0, typegoose_1.pre)("remove", function () { + var _this = this; + Promise.allSettled(this.resolvedInvites.map(function (invite) { return __awaiter(_this, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, invite_1.InviteModel.fetchInvite(invite)]; + case 1: + (_a = (_b.sent())) === null || _a === void 0 ? void 0 : _a.revoke(); + return [2 /*return*/]; + } + }); + }); })); + }) + ], User); + return User; +}()); +exports.User = User; +exports.UserModel = (0, typegoose_1.getModelForClass)(User); diff --git a/dist/types/agerange.d.ts b/dist/types/agerange.d.ts new file mode 100644 index 0000000..f4859d7 --- /dev/null +++ b/dist/types/agerange.d.ts @@ -0,0 +1,9 @@ +export declare enum AgeRange { + unspecified = 1, + from0to10 = 2, + from11to15 = 3, + from16to20 = 4, + from21to30 = 5, + from31to40 = 6 +} +export declare const isValidAgeRange: (value: number) => value is AgeRange; diff --git a/dist/types/agerange.js b/dist/types/agerange.js new file mode 100644 index 0000000..31d8ad7 --- /dev/null +++ b/dist/types/agerange.js @@ -0,0 +1,16 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.isValidAgeRange = exports.AgeRange = void 0; +var AgeRange; +(function (AgeRange) { + AgeRange[AgeRange["unspecified"] = 1] = "unspecified"; + AgeRange[AgeRange["from0to10"] = 2] = "from0to10"; + AgeRange[AgeRange["from11to15"] = 3] = "from11to15"; + AgeRange[AgeRange["from16to20"] = 4] = "from16to20"; + AgeRange[AgeRange["from21to30"] = 5] = "from21to30"; + AgeRange[AgeRange["from31to40"] = 6] = "from31to40"; +})(AgeRange = exports.AgeRange || (exports.AgeRange = {})); +var isValidAgeRange = function (value) { + return !!AgeRange[value]; +}; +exports.isValidAgeRange = isValidAgeRange;