@sphereon/ssi-sdk.data-store
Version:
362 lines • 23.9 kB
JavaScript
"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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IssuanceBrandingStore = void 0;
const debug_1 = __importDefault(require("debug"));
const typeorm_1 = require("typeorm");
const MappingUtils_1 = require("../utils/issuanceBranding/MappingUtils");
const BackgroundAttributesEntity_1 = require("../entities/issuanceBranding/BackgroundAttributesEntity");
const ImageAttributesEntity_1 = require("../entities/issuanceBranding/ImageAttributesEntity");
const ImageDimensionsEntity_1 = require("../entities/issuanceBranding/ImageDimensionsEntity");
const IssuerBrandingEntity_1 = require("../entities/issuanceBranding/IssuerBrandingEntity");
const CredentialBrandingEntity_1 = require("../entities/issuanceBranding/CredentialBrandingEntity");
const CredentialLocaleBrandingEntity_1 = require("../entities/issuanceBranding/CredentialLocaleBrandingEntity");
const IssuerLocaleBrandingEntity_1 = require("../entities/issuanceBranding/IssuerLocaleBrandingEntity");
const TextAttributesEntity_1 = require("../entities/issuanceBranding/TextAttributesEntity");
const AbstractIssuanceBrandingStore_1 = require("./AbstractIssuanceBrandingStore");
const debug = (0, debug_1.default)('sphereon:ssi-sdk:issuance-branding-store');
class IssuanceBrandingStore extends AbstractIssuanceBrandingStore_1.AbstractIssuanceBrandingStore {
constructor(dbConnection) {
super();
this.addCredentialBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { localeBranding, vcHash } = args;
const repository = (yield this.dbConnection).getRepository(CredentialBrandingEntity_1.CredentialBrandingEntity);
const result = yield repository.findOne({
where: [{ vcHash }],
});
if (result) {
return Promise.reject(Error(`Credential branding already present for vc with hash: ${vcHash}`));
}
if (yield this.hasDuplicateLocales(localeBranding)) {
return Promise.reject(Error(`Credential branding contains duplicate locales`));
}
const credentialBrandingEntity = (0, MappingUtils_1.credentialBrandingEntityFrom)(args);
debug('Adding credential branding', credentialBrandingEntity);
const createdResult = yield repository.save(credentialBrandingEntity);
return (0, MappingUtils_1.credentialBrandingFrom)(createdResult);
});
this.getCredentialBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args !== null && args !== void 0 ? args : {};
if (filter) {
filter.forEach((filter) => {
if (filter.localeBranding && 'locale' in filter.localeBranding && filter.localeBranding.locale === undefined) {
filter.localeBranding.locale = '';
}
});
}
debug('Getting credential branding', args);
const result = yield (yield this.dbConnection).getRepository(CredentialBrandingEntity_1.CredentialBrandingEntity).find(Object.assign({}, (filter && { where: filter })));
return result.map((credentialBranding) => (0, MappingUtils_1.credentialBrandingFrom)(credentialBranding));
});
this.removeCredentialBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args;
const repository = (yield this.dbConnection).getRepository(CredentialBrandingEntity_1.CredentialBrandingEntity);
const credentialBranding = yield repository.find({
where: filter,
});
debug('Removing credential locale branding', args);
const localeBrandingDeletions = credentialBranding.map((credentialBranding) => credentialBranding.localeBranding.map((localeBranding) => __awaiter(this, void 0, void 0, function* () { return this.removeLocaleBranding(localeBranding); })));
yield Promise.all(localeBrandingDeletions);
debug('Removing credential branding', args);
const credentialBrandingDeletions = filter.map((filter) => __awaiter(this, void 0, void 0, function* () { return yield repository.delete(filter); }));
yield Promise.all(credentialBrandingDeletions);
});
this.updateCredentialBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { credentialBranding } = args;
const repository = (yield this.dbConnection).getRepository(CredentialBrandingEntity_1.CredentialBrandingEntity);
const credentialBrandingEntity = yield repository.findOne({
where: { id: credentialBranding.id },
});
if (!credentialBrandingEntity) {
return Promise.reject(Error(`No credential branding found for id: ${credentialBranding.id}`));
}
const branding = Object.assign(Object.assign({}, credentialBranding), { localeBranding: credentialBrandingEntity.localeBranding });
debug('Updating credential branding', branding);
const result = yield repository.save(branding, { transaction: true });
return (0, MappingUtils_1.credentialBrandingFrom)(result);
});
this.addCredentialLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { credentialBrandingId, localeBranding } = args;
const credentialBrandingRepository = (yield this.dbConnection).getRepository(CredentialBrandingEntity_1.CredentialBrandingEntity);
const credentialBranding = yield credentialBrandingRepository.findOne({
where: { id: credentialBrandingId },
});
if (!credentialBranding) {
return Promise.reject(Error(`No credential branding found for id: ${credentialBrandingId}`));
}
const locales = yield (yield this.dbConnection).getRepository(CredentialLocaleBrandingEntity_1.CredentialLocaleBrandingEntity).find({
where: {
credentialBranding: {
id: credentialBrandingId,
},
locale: (0, typeorm_1.In)(localeBranding.map((localeBranding) => localeBranding.locale)),
},
});
if (locales && locales.length > 0) {
return Promise.reject(Error(`Credential branding already contains locales: ${locales === null || locales === void 0 ? void 0 : locales.map((credentialLocaleBrandingEntity) => credentialLocaleBrandingEntity.locale)}`));
}
const credentialLocaleBrandingRepository = (yield this.dbConnection).getRepository(CredentialLocaleBrandingEntity_1.CredentialLocaleBrandingEntity);
const addCredentialLocaleBranding = localeBranding.map((localeBranding) => __awaiter(this, void 0, void 0, function* () {
const credentialLocaleBrandingEntity = (0, MappingUtils_1.credentialLocaleBrandingEntityFrom)(localeBranding);
debug('Adding credential locale branding', credentialLocaleBrandingEntity);
credentialLocaleBrandingEntity.credentialBranding = credentialBranding;
yield credentialLocaleBrandingRepository.save(credentialLocaleBrandingEntity, { transaction: true });
}));
yield Promise.all(addCredentialLocaleBranding);
const result = yield credentialBrandingRepository.findOne({
where: { id: credentialBrandingId },
});
if (!result) {
return Promise.reject(Error('Unable to get updated credential branding'));
}
return (0, MappingUtils_1.credentialBrandingFrom)(result);
});
this.getCredentialLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args !== null && args !== void 0 ? args : {};
if (filter) {
filter.forEach((filter) => {
if ('locale' in filter && filter.locale === undefined) {
filter.locale = '';
}
});
}
debug('Getting credential locale branding', args);
const credentialBrandingLocale = yield (yield this.dbConnection)
.getRepository(CredentialLocaleBrandingEntity_1.CredentialLocaleBrandingEntity)
.find(Object.assign({}, (filter && { where: filter })));
return credentialBrandingLocale
? credentialBrandingLocale.map((credentialLocaleBranding) => (0, MappingUtils_1.localeBrandingFrom)(credentialLocaleBranding))
: [];
});
this.removeCredentialLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args;
const credentialLocaleBranding = yield (yield this.dbConnection)
.getRepository(CredentialLocaleBrandingEntity_1.CredentialLocaleBrandingEntity)
.find({
where: filter,
});
debug('Removing credential locale branding', args);
const localeBrandingDeletions = credentialLocaleBranding.map((localeBranding) => __awaiter(this, void 0, void 0, function* () { return this.removeLocaleBranding(localeBranding); }));
yield Promise.all(localeBrandingDeletions);
});
this.updateCredentialLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { localeBranding } = args;
const repository = (yield this.dbConnection).getRepository(CredentialLocaleBrandingEntity_1.CredentialLocaleBrandingEntity);
const result = yield repository.findOne({
where: { id: localeBranding.id },
});
if (!result) {
return Promise.reject(Error(`No credential locale branding found for id: ${localeBranding.id}`));
}
const locales = yield repository.find({
where: {
credentialBranding: {
id: result.credentialBrandingId,
},
id: (0, typeorm_1.Not)((0, typeorm_1.In)([localeBranding.id])),
locale: localeBranding.locale,
},
});
if (locales && locales.length > 0) {
return Promise.reject(Error(`Credential branding: ${result.credentialBrandingId} already contains locale: ${localeBranding.locale}`));
}
debug('Updating credential locale branding', localeBranding);
const updatedResult = yield repository.save(localeBranding, { transaction: true });
return (0, MappingUtils_1.localeBrandingFrom)(updatedResult);
});
this.addIssuerBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { localeBranding, issuerCorrelationId } = args;
const repository = (yield this.dbConnection).getRepository(IssuerBrandingEntity_1.IssuerBrandingEntity);
const result = yield repository.findOne({
where: [{ issuerCorrelationId }],
});
if (result) {
return Promise.reject(Error(`Issuer branding already present for issuer with correlation id: ${issuerCorrelationId}`));
}
if (yield this.hasDuplicateLocales(localeBranding)) {
return Promise.reject(Error(`Issuer branding contains duplicate locales`));
}
const issuerBrandingEntity = (0, MappingUtils_1.issuerBrandingEntityFrom)(args);
debug('Adding issuer branding', issuerBrandingEntity);
const createdResult = yield repository.save(issuerBrandingEntity);
return (0, MappingUtils_1.issuerBrandingFrom)(createdResult);
});
this.getIssuerBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args !== null && args !== void 0 ? args : {};
if (filter) {
filter.forEach((filter) => {
if (filter.localeBranding && 'locale' in filter.localeBranding && filter.localeBranding.locale === undefined) {
filter.localeBranding.locale = '';
}
});
}
debug('Getting issuer branding', args);
const result = yield (yield this.dbConnection).getRepository(IssuerBrandingEntity_1.IssuerBrandingEntity).find(Object.assign({}, (filter && { where: filter })));
return result.map((issuerBranding) => (0, MappingUtils_1.issuerBrandingFrom)(issuerBranding));
});
this.removeIssuerBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args;
const repository = (yield this.dbConnection).getRepository(IssuerBrandingEntity_1.IssuerBrandingEntity);
const issuerBranding = yield repository.find({
where: filter,
});
debug('Removing issuer locale branding', args);
const localeBrandingDeletions = issuerBranding.map((issuerBranding) => issuerBranding.localeBranding.map((localeBranding) => __awaiter(this, void 0, void 0, function* () { return this.removeLocaleBranding(localeBranding); })));
yield Promise.all(localeBrandingDeletions);
debug('Removing issuer branding', args);
const issuerBrandingDeletions = filter.map((filter) => __awaiter(this, void 0, void 0, function* () { return yield repository.delete(filter); }));
yield Promise.all(issuerBrandingDeletions);
});
this.updateIssuerBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { issuerBranding } = args;
const repository = (yield this.dbConnection).getRepository(IssuerBrandingEntity_1.IssuerBrandingEntity);
const issuerBrandingEntity = yield repository.findOne({
where: { id: issuerBranding.id },
});
if (!issuerBrandingEntity) {
return Promise.reject(Error(`No issuer branding found for id: ${issuerBranding.id}`));
}
const branding = Object.assign(Object.assign({}, issuerBranding), { localeBranding: issuerBrandingEntity.localeBranding });
debug('Updating issuer branding', branding);
const result = yield repository.save(branding, { transaction: true });
return (0, MappingUtils_1.issuerBrandingFrom)(result);
});
this.addIssuerLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { localeBranding, issuerBrandingId } = args;
const issuerBrandingRepository = (yield this.dbConnection).getRepository(IssuerBrandingEntity_1.IssuerBrandingEntity);
const issuerBranding = yield issuerBrandingRepository.findOne({
where: { id: issuerBrandingId },
});
if (!issuerBranding) {
return Promise.reject(Error(`No issuer branding found for id: ${issuerBrandingId}`));
}
const locales = yield (yield this.dbConnection).getRepository(IssuerLocaleBrandingEntity_1.IssuerLocaleBrandingEntity).find({
where: {
issuerBranding: {
id: issuerBrandingId,
},
locale: (0, typeorm_1.In)(localeBranding.map((localeBranding) => localeBranding.locale)),
},
});
if (locales && locales.length > 0) {
return Promise.reject(Error(`Issuer branding already contains locales: ${locales === null || locales === void 0 ? void 0 : locales.map((issuerLocaleBrandingEntity) => issuerLocaleBrandingEntity.locale)}`));
}
const issuerLocaleBrandingRepository = (yield this.dbConnection).getRepository(IssuerLocaleBrandingEntity_1.IssuerLocaleBrandingEntity);
const addIssuerLocaleBranding = localeBranding.map((localeBranding) => __awaiter(this, void 0, void 0, function* () {
const issuerLocaleBrandingEntity = (0, MappingUtils_1.issuerLocaleBrandingEntityFrom)(localeBranding);
debug('Adding issuer locale branding', issuerLocaleBrandingEntity);
issuerLocaleBrandingEntity.issuerBranding = issuerBranding;
yield issuerLocaleBrandingRepository.save(issuerLocaleBrandingEntity, { transaction: true });
}));
yield Promise.all(addIssuerLocaleBranding);
const result = yield issuerBrandingRepository.findOne({
where: { id: issuerBrandingId },
});
if (!result) {
return Promise.reject(Error('Unable to get updated issuer branding'));
}
return (0, MappingUtils_1.issuerBrandingFrom)(result);
});
this.getIssuerLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args !== null && args !== void 0 ? args : {};
if (filter) {
filter.forEach((filter) => {
if ('locale' in filter && filter.locale === undefined) {
filter.locale = '';
}
});
}
debug('Getting issuer locale branding', args);
const issuerLocaleBranding = yield (yield this.dbConnection)
.getRepository(IssuerLocaleBrandingEntity_1.IssuerLocaleBrandingEntity)
.find(Object.assign({}, (filter && { where: filter })));
return issuerLocaleBranding
? issuerLocaleBranding.map((issuerLocaleBranding) => (0, MappingUtils_1.localeBrandingFrom)(issuerLocaleBranding))
: [];
});
this.removeIssuerLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { filter } = args;
const issuerLocaleBranding = yield (yield this.dbConnection).getRepository(IssuerLocaleBrandingEntity_1.IssuerLocaleBrandingEntity).find({
where: filter,
});
debug('Removing credential locale branding', args);
const localeBrandingDeletions = issuerLocaleBranding.map((localeBranding) => __awaiter(this, void 0, void 0, function* () { return this.removeLocaleBranding(localeBranding); }));
yield Promise.all(localeBrandingDeletions);
});
this.updateIssuerLocaleBranding = (args) => __awaiter(this, void 0, void 0, function* () {
const { localeBranding } = args;
const repository = (yield this.dbConnection).getRepository(IssuerLocaleBrandingEntity_1.IssuerLocaleBrandingEntity);
const result = yield repository.findOne({
where: { id: localeBranding.id },
});
if (!result) {
return Promise.reject(Error(`No issuer locale branding found for id: ${localeBranding.id}`));
}
const locales = yield repository.find({
where: {
issuerBranding: {
id: result.issuerBrandingId,
},
id: (0, typeorm_1.Not)((0, typeorm_1.In)([localeBranding.id])),
locale: localeBranding.locale,
},
});
if (locales && locales.length > 0) {
return Promise.reject(Error(`Issuer branding: ${result.issuerBrandingId} already contains locale: ${localeBranding.locale}`));
}
debug('Updating issuer locale branding', localeBranding);
const updatedResult = yield repository.save(localeBranding, { transaction: true });
return (0, MappingUtils_1.localeBrandingFrom)(updatedResult);
});
this.hasDuplicateLocales = (localeBranding) => __awaiter(this, void 0, void 0, function* () {
let seen = new Set();
return localeBranding.some((localeBranding) => {
return seen.size === seen.add(localeBranding.locale).size;
});
});
this.removeLocaleBranding = (localeBranding) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
debug('Removing credential locale branding', localeBranding);
// Delete background image dimensions
if ((_b = (_a = localeBranding.background) === null || _a === void 0 ? void 0 : _a.image) === null || _b === void 0 ? void 0 : _b.dimensions) {
yield (yield this.dbConnection).getRepository(ImageDimensionsEntity_1.ImageDimensionsEntity).delete({ id: (_e = (_d = (_c = localeBranding.background) === null || _c === void 0 ? void 0 : _c.image) === null || _d === void 0 ? void 0 : _d.dimensions) === null || _e === void 0 ? void 0 : _e.id });
}
// Delete background image
if ((_f = localeBranding.background) === null || _f === void 0 ? void 0 : _f.image) {
yield (yield this.dbConnection).getRepository(ImageAttributesEntity_1.ImageAttributesEntity).delete({ id: (_h = (_g = localeBranding.background) === null || _g === void 0 ? void 0 : _g.image) === null || _h === void 0 ? void 0 : _h.id });
}
// Delete background
if (localeBranding.background) {
yield (yield this.dbConnection).getRepository(BackgroundAttributesEntity_1.BackgroundAttributesEntity).delete({ id: (_j = localeBranding.background) === null || _j === void 0 ? void 0 : _j.id });
}
// Delete logo image dimensions
if ((_k = localeBranding.logo) === null || _k === void 0 ? void 0 : _k.dimensions) {
yield (yield this.dbConnection).getRepository(ImageDimensionsEntity_1.ImageDimensionsEntity).delete({ id: (_m = (_l = localeBranding.logo) === null || _l === void 0 ? void 0 : _l.dimensions) === null || _m === void 0 ? void 0 : _m.id });
}
// Delete logo
if (localeBranding.logo) {
yield (yield this.dbConnection).getRepository(ImageAttributesEntity_1.ImageAttributesEntity).delete({ id: (_o = localeBranding.logo) === null || _o === void 0 ? void 0 : _o.id });
}
// Delete text
if (localeBranding.text) {
yield (yield this.dbConnection).getRepository(TextAttributesEntity_1.TextAttributesEntity).delete({ id: (_p = localeBranding.text) === null || _p === void 0 ? void 0 : _p.id });
}
// Delete locale branding
yield (yield this.dbConnection).getRepository(CredentialLocaleBrandingEntity_1.CredentialLocaleBrandingEntity).delete({ id: localeBranding.id });
});
this.dbConnection = dbConnection;
}
}
exports.IssuanceBrandingStore = IssuanceBrandingStore;
//# sourceMappingURL=IssuanceBrandingStore.js.map