Files

142 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2020-02-19 15:55:06 +11:00
// Objection Docs:
// http://vincit.github.io/objection.js/
2025-09-02 21:43:00 +10:00
import { Model } from "objection";
import db from "../db.js";
import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js";
import deadHostModel from "./dead_host.js";
import now from "./now_helper.js";
import proxyHostModel from "./proxy_host.js";
import redirectionHostModel from "./redirection_host.js";
2025-10-28 23:10:00 +10:00
import streamModel from "./stream.js";
2025-09-02 21:43:00 +10:00
import userModel from "./user.js";
2020-02-19 15:55:06 +11:00
Model.knex(db());
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
const boolFields = ["is_deleted"];
2024-10-10 15:53:11 +10:00
2026-05-18 15:04:27 +10:00
const cleanDomainNames = (domainNames) => {
// Sort domain_names
if (typeof domainNames !== "undefined") {
const newDomainNames = domainNames.filter((name) => name != null);
2026-05-18 15:04:27 +10:00
newDomainNames.sort();
return newDomainNames;
}
return [];
};
2020-02-19 15:55:06 +11:00
class Certificate extends Model {
2025-09-02 21:43:00 +10:00
$beforeInsert() {
this.created_on = now();
this.modified_on = now();
2020-02-19 15:55:06 +11:00
// Default for expires_on
2025-09-02 21:43:00 +10:00
if (typeof this.expires_on === "undefined") {
this.expires_on = now();
2020-02-19 15:55:06 +11:00
}
// Default for domain_names
if (typeof this.domain_names !== "undefined") {
this.domain_names = cleanDomainNames(this.domain_names);
} else {
this.domain_names = [];
}
2020-02-19 15:55:06 +11:00
// Default for meta
2025-09-02 21:43:00 +10:00
if (typeof this.meta === "undefined") {
2020-02-19 15:55:06 +11:00
this.meta = {};
}
}
2025-09-02 21:43:00 +10:00
$beforeUpdate() {
this.modified_on = now();
if (typeof this.domain_names !== "undefined") {
this.domain_names = cleanDomainNames(this.domain_names);
}
2020-02-19 15:55:06 +11:00
}
2024-10-10 15:53:11 +10:00
$parseDatabaseJson(json) {
2025-09-02 21:43:00 +10:00
const thisJson = super.$parseDatabaseJson(json);
return convertIntFieldsToBool(thisJson, boolFields);
2024-10-10 15:53:11 +10:00
}
$formatDatabaseJson(json) {
2025-09-02 21:43:00 +10:00
const thisJson = convertBoolFieldsToInt(json, boolFields);
return super.$formatDatabaseJson(thisJson);
2024-10-10 15:53:11 +10:00
}
2025-09-02 21:43:00 +10:00
static get name() {
return "Certificate";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get tableName() {
return "certificate";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get jsonAttributes() {
return ["domain_names", "meta"];
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get relationMappings() {
2020-02-19 15:55:06 +11:00
return {
owner: {
2025-09-02 21:43:00 +10:00
relation: Model.HasOneRelation,
modelClass: userModel,
join: {
from: "certificate.owner_user_id",
to: "user.id",
},
modify: (qb) => {
qb.where("user.is_deleted", 0);
2020-02-19 15:55:06 +11:00
},
},
proxy_hosts: {
2025-09-02 21:43:00 +10:00
relation: Model.HasManyRelation,
modelClass: proxyHostModel,
join: {
from: "certificate.id",
to: "proxy_host.certificate_id",
},
modify: (qb) => {
qb.where("proxy_host.is_deleted", 0);
},
},
dead_hosts: {
2025-09-02 21:43:00 +10:00
relation: Model.HasManyRelation,
modelClass: deadHostModel,
join: {
from: "certificate.id",
to: "dead_host.certificate_id",
},
modify: (qb) => {
qb.where("dead_host.is_deleted", 0);
},
},
redirection_hosts: {
2025-09-02 21:43:00 +10:00
relation: Model.HasManyRelation,
modelClass: redirectionHostModel,
join: {
from: "certificate.id",
to: "redirection_host.certificate_id",
},
modify: (qb) => {
qb.where("redirection_host.is_deleted", 0);
},
2025-09-02 21:43:00 +10:00
},
2025-10-28 23:10:00 +10:00
streams: {
relation: Model.HasManyRelation,
modelClass: streamModel,
join: {
from: "certificate.id",
to: "stream.certificate_id",
},
modify: (qb) => {
qb.where("stream.is_deleted", 0);
},
},
2020-02-19 15:55:06 +11:00
};
}
}
2025-09-02 21:43:00 +10:00
export default Certificate;