2022-03-05 22:57:23 +04:00
|
|
|
import crypto from '../utils/crypto';
|
|
|
|
import database from '../utils/database/index';
|
|
|
|
|
|
|
|
const binaryMD5 = crypto.binaryMD5;
|
|
|
|
const aliasesDb = database['aliases'];
|
2019-01-25 02:23:00 +03:00
|
|
|
|
|
|
|
/**
|
2020-05-09 05:38:25 +03:00
|
|
|
* @typedef {object} AliasData
|
2019-01-25 02:23:00 +03:00
|
|
|
* @property {string} _id - alias id
|
|
|
|
* @property {string} hash - alias binary hash
|
|
|
|
* @property {string} type - entity type
|
|
|
|
* @property {boolean} deprecated - indicate if alias deprecated
|
|
|
|
* @property {string} id - entity id
|
|
|
|
*
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
export interface AliasData {
|
|
|
|
_id?: string;
|
|
|
|
hash?: string;
|
|
|
|
type?: string;
|
|
|
|
deprecated?: boolean;
|
|
|
|
id?: string;
|
|
|
|
}
|
2019-01-25 02:23:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Alias
|
2019-01-25 06:19:37 +03:00
|
|
|
* @classdesc Alias model
|
|
|
|
*
|
2019-01-25 02:23:00 +03:00
|
|
|
* @property {string} _id - alias id
|
|
|
|
* @property {string} hash - alias binary hash
|
|
|
|
* @property {string} type - entity type
|
|
|
|
* @property {boolean} deprecated - indicate if alias deprecated
|
|
|
|
* @property {string} id - entity title
|
|
|
|
*/
|
|
|
|
class Alias {
|
2022-03-05 22:57:23 +04:00
|
|
|
public _id?: string;
|
|
|
|
public hash?: string;
|
|
|
|
public type?: string;
|
|
|
|
public deprecated?: boolean;
|
|
|
|
public id?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*
|
|
|
|
* @param {AliasData} data - info about alias
|
|
|
|
* @param {string} aliasName - alias of entity
|
|
|
|
*/
|
|
|
|
constructor(data: AliasData = {}, aliasName = '') {
|
|
|
|
if (data === null) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
if (data._id) {
|
|
|
|
this._id = data._id;
|
|
|
|
}
|
|
|
|
if (aliasName) {
|
|
|
|
this.hash = binaryMD5(aliasName);
|
|
|
|
}
|
|
|
|
this.data = data;
|
|
|
|
}
|
2019-01-25 02:23:00 +03:00
|
|
|
/**
|
|
|
|
* Return Alias types
|
|
|
|
*
|
2020-05-09 05:38:25 +03:00
|
|
|
* @returns {object}
|
2019-01-25 02:23:00 +03:00
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public static get types(): { PAGE: string } {
|
2019-01-25 02:23:00 +03:00
|
|
|
return {
|
2020-05-09 05:38:25 +03:00
|
|
|
PAGE: 'page',
|
2019-01-25 02:23:00 +03:00
|
|
|
};
|
2022-03-05 22:57:23 +04:00
|
|
|
}
|
2019-01-25 02:23:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and return alias with given alias
|
2020-05-09 05:38:25 +03:00
|
|
|
*
|
2019-01-25 02:23:00 +03:00
|
|
|
* @param {string} aliasName - alias of entity
|
|
|
|
* @returns {Promise<Alias>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public static async get(aliasName: string): Promise<Alias> {
|
2019-01-25 02:23:00 +03:00
|
|
|
const hash = binaryMD5(aliasName);
|
2020-05-09 05:38:25 +03:00
|
|
|
let data = await aliasesDb.findOne({
|
|
|
|
hash: hash,
|
|
|
|
deprecated: false,
|
|
|
|
});
|
2019-01-25 02:23:00 +03:00
|
|
|
|
|
|
|
if (!data) {
|
2019-02-15 17:56:56 +03:00
|
|
|
data = await aliasesDb.findOne({ hash: hash });
|
2019-01-25 02:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Alias(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-05 22:57:23 +04:00
|
|
|
* Mark alias as deprecated
|
2019-01-25 02:23:00 +03:00
|
|
|
*
|
|
|
|
* @param {string} aliasName - alias of entity
|
2022-03-05 22:57:23 +04:00
|
|
|
* @returns {Promise<Alias>}
|
2019-01-25 02:23:00 +03:00
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public static async markAsDeprecated(aliasName: string): Promise<Alias> {
|
|
|
|
const alias = await Alias.get(aliasName);
|
|
|
|
|
|
|
|
alias.deprecated = true;
|
|
|
|
|
|
|
|
return alias.save();
|
2019-01-25 02:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save or update alias data in the database
|
|
|
|
*
|
|
|
|
* @returns {Promise<Alias>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public async save(): Promise<Alias> {
|
2019-01-25 02:23:00 +03:00
|
|
|
if (!this._id) {
|
2022-03-05 22:57:23 +04:00
|
|
|
const insertedRow = await aliasesDb.insert(this.data) as { _id: string };
|
2019-01-25 02:23:00 +03:00
|
|
|
|
|
|
|
this._id = insertedRow._id;
|
|
|
|
} else {
|
2019-02-15 17:56:56 +03:00
|
|
|
await aliasesDb.update({ _id: this._id }, this.data);
|
2019-01-25 02:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set AliasData object fields to internal model fields
|
|
|
|
*
|
2022-03-05 22:57:23 +04:00
|
|
|
* @param {AliasData} aliasData - info about alias
|
2019-01-25 02:23:00 +03:00
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public set data(aliasData: AliasData) {
|
2019-02-15 17:56:56 +03:00
|
|
|
const { id, type, hash, deprecated } = aliasData;
|
2019-01-25 02:23:00 +03:00
|
|
|
|
|
|
|
this.id = id || this.id;
|
|
|
|
this.type = type || this.type;
|
|
|
|
this.hash = hash || this.hash;
|
|
|
|
this.deprecated = deprecated || false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return AliasData object
|
|
|
|
*
|
|
|
|
* @returns {AliasData}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public get data(): AliasData {
|
2019-01-25 02:23:00 +03:00
|
|
|
return {
|
|
|
|
_id: this._id,
|
|
|
|
id: this.id,
|
|
|
|
type: this.type,
|
|
|
|
hash: this.hash,
|
2020-05-09 05:38:25 +03:00
|
|
|
deprecated: this.deprecated,
|
2019-01-25 02:23:00 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-25 06:19:37 +03:00
|
|
|
/**
|
|
|
|
* @returns {Promise<Alias>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public async destroy(): Promise<Alias> {
|
2019-02-15 17:56:56 +03:00
|
|
|
await aliasesDb.remove({ _id: this._id });
|
2019-01-25 06:19:37 +03:00
|
|
|
|
|
|
|
delete this._id;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2019-01-25 02:23:00 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 22:57:23 +04:00
|
|
|
export default Alias;
|