@sphereon/ssi-sdk.data-store
Version:
243 lines • 12.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.StatusListStore = void 0;
const ssi_types_1 = require("@sphereon/ssi-types");
const debug_1 = __importDefault(require("debug"));
const typeorm_1 = require("typeorm");
const StatusListEntities_1 = require("../entities/statusList/StatusListEntities");
const StatusList2021EntryEntity_1 = require("../entities/statusList/StatusList2021EntryEntity");
const MappingUtils_1 = require("../utils/statusList/MappingUtils");
const debug = (0, debug_1.default)('sphereon:ssi-sdk:data-store:status-list');
class StatusListStore {
constructor(dbConnection) {
this._dbConnection = dbConnection;
}
/**
* Gets the available status list indices from the provided indices. Meaning it will filter out any index that is already known.
*
* The idea is that the caller provides a set of random status list indices. We can relatively easy check against the DB in an optimized way.
* If the status list is large it is probably best to also provide at least a good number of indices. So something like 10 or 20 values.
* Callers are also expected to call this function multiple times if it does not yield results
*
* @param args
*/
availableStatusListEntries(args) {
return __awaiter(this, void 0, void 0, function* () {
const statusListIndex = Array.isArray(args.statusListIndex) ? args.statusListIndex : [args.statusListIndex];
const statusList = yield this.getStatusList(Object.assign(Object.assign({}, args), { id: args.statusListId }));
const repo = yield this.getStatusListEntryRepo();
const results = (yield repo.find({
where: {
statusList,
statusListIndex: (0, typeorm_1.In)(statusListIndex),
},
})).map((index) => index.statusListIndex);
return statusListIndex.filter((index) => !results.includes(index));
});
}
addStatusListEntry(args) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.getStatusListEntryRepo()).save(args);
});
}
updateStatusListEntry(args) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const statusListId = (_a = args.statusListId) !== null && _a !== void 0 ? _a : (_b = args.statusList) === null || _b === void 0 ? void 0 : _b.id;
const result = yield this.getStatusListEntryByIndex(Object.assign(Object.assign({}, args), { statusListId, errorOnNotFound: false }));
const updatedEntry = {
value: args.value,
correlationId: args.correlationId,
credentialHash: args.credentialHash,
credentialId: args.credentialId,
};
const updStatusListId = (_c = result === null || result === void 0 ? void 0 : result.statusListId) !== null && _c !== void 0 ? _c : statusListId;
const updateResult = yield (yield this.getStatusListEntryRepo()).upsert(Object.assign(Object.assign({}, (result !== null && result !== void 0 ? result : { statusListId: updStatusListId, statusListIndex: args.statusListIndex })), updatedEntry), { conflictPaths: ['statusList', 'statusListIndex'] });
console.log(updateResult);
return (yield this.getStatusListEntryByIndex(Object.assign(Object.assign({}, args), { statusListId: updStatusListId, errorOnNotFound: true })));
});
}
getStatusListEntryByIndex(_a) {
return __awaiter(this, arguments, void 0, function* ({ statusListId, statusListCorrelationId, statusListIndex, entryCorrelationId, errorOnNotFound, }) {
if (!statusListId && !statusListCorrelationId) {
throw Error(`Cannot get statusList entry without either a statusList id or statusListCorrelationId`);
}
if (!statusListIndex && !entryCorrelationId) {
throw Error(`Cannot get statusList entry without either a statusListIndex or entryCorrelationId`);
}
const result = yield (yield this.getStatusListEntryRepo()).findOne({
where: Object.assign(Object.assign(Object.assign(Object.assign({}, (statusListId && { statusListId })), (!statusListId && statusListCorrelationId && { statusList: { correlationId: statusListCorrelationId } })), (statusListIndex && { statusListIndex })), (entryCorrelationId && { entryCorrelationId })),
relations: {
statusList: true,
},
});
if (!result && errorOnNotFound) {
throw Error(`Could not find status list entry with provided filters`);
}
return result !== null && result !== void 0 ? result : undefined;
});
}
getStatusListEntryByCredentialId(args) {
return __awaiter(this, void 0, void 0, function* () {
const credentialId = args.credentialId;
if (!credentialId) {
throw Error('Can only get a credential by credentialId when a credentialId is supplied');
}
const statusList = yield this.getStatusList({
id: args.statusListId,
correlationId: args.statusListCorrelationId,
});
const where = Object.assign(Object.assign({ statusList: { id: statusList.id } }, (args.entryCorrelationId && { correlationId: args.entryCorrelationId })), { credentialId });
console.log(`Entries: ${JSON.stringify(yield (yield this.getStatusListEntryRepo()).find(), null, 2)}`);
const result = yield (yield this.getStatusListEntryRepo()).findOne({ where });
if (!result && args.errorOnNotFound) {
throw Error(`Could not find status list credential id ${credentialId} for status list id ${statusList.id}`);
}
return result !== null && result !== void 0 ? result : undefined;
});
}
removeStatusListEntryByCredentialId(args) {
return __awaiter(this, void 0, void 0, function* () {
let error = false;
try {
yield this.getStatusListEntryByCredentialId(args); // only used to check it exists
}
catch (error) {
error = true;
}
if (!error) {
const result = yield (yield this.getStatusListEntryRepo()).delete(Object.assign(Object.assign(Object.assign({}, (args.statusListId && { statusList: args.statusListId })), (args.entryCorrelationId && { correlationId: args.entryCorrelationId })), { credentialId: args.credentialId }));
error = !result.affected || result.affected !== 1;
}
return !error;
});
}
removeStatusListEntryByIndex(args) {
return __awaiter(this, void 0, void 0, function* () {
let error = false;
try {
yield this.getStatusListEntryByIndex(args);
}
catch (error) {
error = true;
}
if (error) {
console.log(`Could not delete statusList ${args.statusListId} entry by index ${args.statusListIndex}`);
}
else {
const result = yield (yield this.getStatusListEntryRepo()).delete(Object.assign(Object.assign(Object.assign({}, (args.statusListId && { statusList: args.statusListId })), (args.entryCorrelationId && { correlationId: args.entryCorrelationId })), { statusListIndex: args.statusListIndex }));
error = !result.affected || result.affected !== 1;
}
return !error;
});
}
getStatusListEntries(args) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.getStatusListEntryRepo()).find({ where: Object.assign(Object.assign({}, args === null || args === void 0 ? void 0 : args.filter), { statusList: args.statusListId }) });
});
}
getStatusList(args) {
return __awaiter(this, void 0, void 0, function* () {
return (0, MappingUtils_1.statusListFrom)(yield this.getStatusListEntity(args));
});
}
getStatusListEntity(args) {
return __awaiter(this, void 0, void 0, function* () {
if (!args.id && !args.correlationId) {
throw Error(`At least and 'id' or 'correlationId' needs to be provided to lookup a status list`);
}
const where = [];
if (args.id) {
where.push({ id: args.id });
}
else if (args.correlationId) {
where.push({ correlationId: args.correlationId });
}
const result = yield (yield this.getStatusListRepo()).findOne({ where });
if (!result) {
throw Error(`No status list found for id ${args.id}`);
}
return result;
});
}
getStatusLists(args) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield (yield this.getStatusListRepo()).find({
where: args.filter,
});
if (!result) {
return [];
}
return result.map((entity) => (0, MappingUtils_1.statusListFrom)(entity));
});
}
addStatusList(args) {
return __awaiter(this, void 0, void 0, function* () {
const { id, correlationId } = args;
const result = yield (yield this.getStatusListRepo()).findOne({
where: [{ id }, { correlationId }],
});
if (result) {
throw Error(`Status list for id ${id}, correlationId ${correlationId} already exists`);
}
debug('Adding status list ', id);
const entity = (0, MappingUtils_1.statusListEntityFrom)(args);
const createdResult = yield (yield this.getStatusListRepo(args.type)).save(entity);
return (0, MappingUtils_1.statusListFrom)(createdResult);
});
}
updateStatusList(args) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.getStatusList(args);
debug('Updating status list', result);
const entity = (0, MappingUtils_1.statusListEntityFrom)(args);
const updatedResult = yield (yield this.getStatusListRepo(args.type)).save(entity, { transaction: true });
return (0, MappingUtils_1.statusListFrom)(updatedResult);
});
}
removeStatusList(args) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.getStatusListEntity(args);
yield (yield this.getStatusListEntryRepo()).delete({ statusListId: result.id });
const deletedEntity = yield (yield this.getStatusListRepo()).remove(result);
return Boolean(deletedEntity);
});
}
getDS() {
return __awaiter(this, void 0, void 0, function* () {
return this._dbConnection;
});
}
getStatusListRepo(type) {
return __awaiter(this, void 0, void 0, function* () {
const dataSource = yield this.getDS();
switch (type) {
case ssi_types_1.StatusListType.StatusList2021:
return dataSource.getRepository(StatusListEntities_1.StatusList2021Entity);
case ssi_types_1.StatusListType.OAuthStatusList:
return dataSource.getRepository(StatusListEntities_1.OAuthStatusListEntity);
default:
return dataSource.getRepository(StatusListEntities_1.StatusListEntity);
}
});
}
getStatusListEntryRepo() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.getDS()).getRepository(StatusList2021EntryEntity_1.StatusListEntryEntity);
});
}
}
exports.StatusListStore = StatusListStore;
//# sourceMappingURL=StatusListStore.js.map