You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.6 KiB
72 lines
1.6 KiB
// domain.js
|
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const striptags = require('striptags');
|
|
|
|
const mongoose = require('mongoose');
|
|
const Domain = mongoose.model('Domain');
|
|
|
|
const { SiteService, SiteError } = require('../../lib/site-lib');
|
|
|
|
class DomainService extends SiteService {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
}
|
|
|
|
async create (domainDefinition) {
|
|
const NOW = new Date();
|
|
const domain = new Domain();
|
|
domain.created = NOW;
|
|
domain.name = striptags(domainDefinition.name.trim().toLowerCase());
|
|
await domain.save();
|
|
return domain.toObject();
|
|
}
|
|
|
|
async update (domain, domainDefinition) {
|
|
await Domain.updateOne(
|
|
{ _id: domain._id },
|
|
{
|
|
$set: {
|
|
name: striptags(domainDefinition.name.trim().toLowerCase()),
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
async getSiteDomain ( ) {
|
|
return this.getByName(process.env.DTP_SITE_DOMAIN_KEY);
|
|
}
|
|
|
|
async getById (domainId) {
|
|
const domain = await Domain.findById(domainId).lean();
|
|
return domain;
|
|
}
|
|
|
|
async getByName (domainName) {
|
|
const domain = await Domain.findOne({ name: domainName.toLowerCase().trim() }).lean();
|
|
if (!domain) {
|
|
throw new SiteError(404, 'Domain does not exist');
|
|
}
|
|
return domain;
|
|
}
|
|
|
|
async getDomains (pagination) {
|
|
const domains = await Domain
|
|
.find()
|
|
.sort({ name: 1 })
|
|
.skip(pagination.skip)
|
|
.limit(pagination.cpp)
|
|
.lean();
|
|
return domains;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'domain',
|
|
name: 'domain',
|
|
create: (dtp) => { return new DomainService(dtp); },
|
|
};
|