UNPKG

@wepublish/api

Version:
103 lines 4.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventsImportService = exports.EVENT_IMPORT_PROVIDER = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const client_1 = require("@prisma/client"); exports.EVENT_IMPORT_PROVIDER = Symbol('Event Import Provider'); let EventsImportService = exports.EventsImportService = class EventsImportService { constructor(providers, prisma) { this.providers = providers; this.prisma = prisma; } importedEvents({ filter, skip, take }) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const importableEvents = Promise.all(this.providers.map((provider) => tslib_1.__awaiter(this, void 0, void 0, function* () { return yield provider.importedEvents(); }))); try { const events = yield importableEvents; const flattened = events.flat(); // sort events let sortedEvents = flattened.sort((a, b) => { // by startsAt date by default return +a.startsAt - +b.startsAt; }); // apply filters to events if (filter.providers && filter.providers.length) { sortedEvents = sortedEvents.filter(e => { var _a; return e.externalSourceName && ((_a = filter === null || filter === void 0 ? void 0 : filter.providers) === null || _a === void 0 ? void 0 : _a.includes(e.externalSourceName)); }); } if (filter.from) { sortedEvents = sortedEvents.filter(e => e.startsAt > new Date(filter.from)); } if (filter.name) { const nameFilter = filter.name.toLowerCase(); sortedEvents = sortedEvents.filter(e => e.name.toLowerCase().includes(nameFilter)); } if (filter.location) { const locationFilter = filter.location.toLowerCase(); sortedEvents = sortedEvents.filter(e => { var _a; return (_a = e.location) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(locationFilter); }); } if (filter.to) { sortedEvents = sortedEvents .filter(e => e.endsAt) .filter(e => e.endsAt < new Date(filter.to)); } // apply pagination const paginatedEvents = sortedEvents.slice(skip, skip + take); const firstEvent = sortedEvents[0]; const lastEvent = sortedEvents[sortedEvents.length - 1]; const aggregated = { nodes: paginatedEvents, totalCount: sortedEvents.length, pageInfo: { hasPreviousPage: false, hasNextPage: false, startCursor: firstEvent === null || firstEvent === void 0 ? void 0 : firstEvent.id, endCursor: lastEvent === null || lastEvent === void 0 ? void 0 : lastEvent.id } }; return aggregated; } catch (e) { throw new Error(e); } }); } importedEvent(filter) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const { id, source } = filter; return (_a = this.providers.find(p => p.name === source)) === null || _a === void 0 ? void 0 : _a.importedEvent({ id }); }); } createEventFromSource({ id, source }) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { return (_a = this.providers.find(p => p.name === source)) === null || _a === void 0 ? void 0 : _a.createEvent({ id }); }); } importedEventsIds() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const externalEventsIds = this.prisma.event .findMany({ where: { externalSourceId: { not: null } } }) .then(res => res.map(single => single.externalSourceId)); return externalEventsIds; }); } getProviders() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.providers.map(provider => provider.name); }); } }; exports.EventsImportService = EventsImportService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__param(0, (0, common_1.Inject)(exports.EVENT_IMPORT_PROVIDER)), tslib_1.__metadata("design:paramtypes", [Array, client_1.PrismaClient]) ], EventsImportService); //# sourceMappingURL=events-import.service.js.map