UNPKG

@authup/server-api

Version:

This is a standalone application.

73 lines 2.78 kB
"use strict"; /* * Copyright (c) 2021-2021. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.IdentityProviderRepository = void 0; const core_1 = require("@authup/core"); const typeorm_1 = require("typeorm"); const entity_1 = require("./entity"); const utils_1 = require("../utils"); const identity_provider_attribute_1 = require("../identity-provider-attribute"); class IdentityProviderRepository extends typeorm_1.Repository { constructor(instance) { super(entity_1.IdentityProviderEntity, typeorm_1.InstanceChecker.isDataSource(instance) ? instance.manager : instance); } async getAttributes(id) { const repository = this.manager.getRepository(identity_provider_attribute_1.IdentityProviderAttributeEntity); const entities = await repository.find({ where: { provider_id: id, }, }); return (0, utils_1.transformAttributesToRecord)(entities); } async saveAttributes(id, attributes) { const repository = this.manager.getRepository(identity_provider_attribute_1.IdentityProviderAttributeEntity); let entities = await repository.find({ where: { provider_id: id, }, }); const reamingAttributes = { ...attributes, }; const entitiesToRemove = []; for (let i = 0; i < entities.length; i++) { if ((0, core_1.hasOwnProperty)(reamingAttributes, entities[i].name)) { entities[i].value = reamingAttributes[entities[i].name]; delete reamingAttributes[entities[i].name]; } else { entitiesToRemove.push(entities[i]); } } await repository.save(entities); if (entitiesToRemove.length > 0) { await repository.remove(entitiesToRemove); } entities = (0, utils_1.transformAttributesToEntities)(reamingAttributes, { provider_id: id, }); if (entities.length > 0) { await repository.insert(entities); } } appendAttributes(entity, attributes) { const keys = Object.keys(attributes); for (let i = 0; i < keys.length; i++) { entity[keys[i]] = attributes[keys[i]]; } return entity; } async extendEntity(entity) { const attributes = await this.getAttributes(entity.id); this.appendAttributes(entity, attributes); return entity; } } exports.IdentityProviderRepository = IdentityProviderRepository; //# sourceMappingURL=repository.js.map