Notes/src/becca/entities/betapi_token.ts

80 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-01-10 17:09:20 +01:00
"use strict";
2024-02-17 01:03:38 +02:00
import { EtapiTokenRow } from "./rows";
2024-02-16 21:38:09 +02:00
const dateUtils = require('../../services/date_utils');
const AbstractBeccaEntity = require('./abstract_becca_entity.js');
2022-01-10 17:09:20 +01:00
/**
* EtapiToken is an entity representing token used to authenticate against Trilium REST API from client applications.
* Used by:
* - Trilium Sender
* - ETAPI clients
*
2022-01-12 19:32:23 +01:00
* The format user is presented with is "<etapiTokenId>_<tokenHash>". This is also called "authToken" to distinguish it
* from tokenHash and token.
2022-04-16 00:17:32 +02:00
*
* @extends AbstractBeccaEntity
2022-01-10 17:09:20 +01:00
*/
class BEtapiToken extends AbstractBeccaEntity {
2022-01-10 17:09:20 +01:00
static get entityName() { return "etapi_tokens"; }
static get primaryKeyName() { return "etapiTokenId"; }
static get hashedProperties() { return ["etapiTokenId", "name", "tokenHash", "utcDateCreated", "utcDateModified", "isDeleted"]; }
2024-02-17 01:03:38 +02:00
etapiTokenId!: string;
name!: string;
tokenHash!: string;
isDeleted!: boolean;
constructor(row: EtapiTokenRow) {
2022-01-10 17:09:20 +01:00
super();
if (!row) {
return;
}
this.updateFromRow(row);
this.init();
}
2024-02-17 01:03:38 +02:00
updateFromRow(row: EtapiTokenRow) {
2022-01-10 17:09:20 +01:00
this.etapiTokenId = row.etapiTokenId;
this.name = row.name;
this.tokenHash = row.tokenHash;
this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime();
this.utcDateModified = row.utcDateModified || this.utcDateCreated;
this.isDeleted = !!row.isDeleted;
2022-02-07 22:50:28 +01:00
if (this.etapiTokenId) {
this.becca.etapiTokens[this.etapiTokenId] = this;
}
2022-01-10 17:09:20 +01:00
}
2022-01-10 17:09:20 +01:00
init() {
if (this.etapiTokenId) {
this.becca.etapiTokens[this.etapiTokenId] = this;
}
}
getPojo() {
return {
etapiTokenId: this.etapiTokenId,
name: this.name,
tokenHash: this.tokenHash,
utcDateCreated: this.utcDateCreated,
utcDateModified: this.utcDateModified,
isDeleted: this.isDeleted
}
}
beforeSaving() {
this.utcDateModified = dateUtils.utcNowDateTime();
super.beforeSaving();
this.becca.etapiTokens[this.etapiTokenId] = this;
}
}
2024-02-17 01:03:38 +02:00
export = BEtapiToken;