certificate.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. const db = require('../db');
  4. const helpers = require('../lib/helpers');
  5. const Model = require('objection').Model;
  6. const User = require('./user');
  7. const now = require('./now_helper');
  8. Model.knex(db);
  9. const boolFields = [
  10. 'is_deleted',
  11. ];
  12. class Certificate extends Model {
  13. $beforeInsert () {
  14. this.created_on = now();
  15. this.modified_on = now();
  16. // Default for expires_on
  17. if (typeof this.expires_on === 'undefined') {
  18. this.expires_on = now();
  19. }
  20. // Default for domain_names
  21. if (typeof this.domain_names === 'undefined') {
  22. this.domain_names = [];
  23. }
  24. // Default for meta
  25. if (typeof this.meta === 'undefined') {
  26. this.meta = {};
  27. }
  28. this.domain_names.sort();
  29. }
  30. $beforeUpdate () {
  31. this.modified_on = now();
  32. // Sort domain_names
  33. if (typeof this.domain_names !== 'undefined') {
  34. this.domain_names.sort();
  35. }
  36. }
  37. $parseDatabaseJson(json) {
  38. json = super.$parseDatabaseJson(json);
  39. return helpers.convertIntFieldsToBool(json, boolFields);
  40. }
  41. $formatDatabaseJson(json) {
  42. json = helpers.convertBoolFieldsToInt(json, boolFields);
  43. return super.$formatDatabaseJson(json);
  44. }
  45. static get name () {
  46. return 'Certificate';
  47. }
  48. static get tableName () {
  49. return 'certificate';
  50. }
  51. static get jsonAttributes () {
  52. return ['domain_names', 'meta'];
  53. }
  54. static get relationMappings () {
  55. return {
  56. owner: {
  57. relation: Model.HasOneRelation,
  58. modelClass: User,
  59. join: {
  60. from: 'certificate.owner_user_id',
  61. to: 'user.id'
  62. },
  63. modify: function (qb) {
  64. qb.where('user.is_deleted', 0);
  65. }
  66. }
  67. };
  68. }
  69. }
  70. module.exports = Certificate;