add insert validation
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
|
||||
const { supabase } = require('../auth');
|
||||
const { isValidDomain } = require('../helpers/utils');
|
||||
|
||||
class SiteModel {
|
||||
|
||||
@@ -17,6 +18,21 @@ class SiteModel {
|
||||
* @returns {Promise<Object>} - The result of the insert operation.
|
||||
*/
|
||||
async insert(name, domainName) {
|
||||
// validate inputs
|
||||
if (!name || !domainName) {
|
||||
throw new Error('Name and domain name are required to insert a site.');
|
||||
}
|
||||
|
||||
// validate domain name format
|
||||
if (!isValidDomain(domainName)) {
|
||||
throw new Error('Invalid domain name format.');
|
||||
}
|
||||
|
||||
// check if the domain name already exists
|
||||
if (await this.getByDomainName(domainName).then(data => data.length !== 0)) {
|
||||
throw new Error('Domain name already exists.');
|
||||
}
|
||||
|
||||
const { data, error } = await supabase.from(SiteModel.tableName).insert({
|
||||
name: name,
|
||||
domain_name: domainName,
|
||||
@@ -64,6 +80,24 @@ class SiteModel {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a site by its domain name.
|
||||
*
|
||||
* @param {string} domainName - The domain name of the site.
|
||||
*
|
||||
* @returns {Promise<Object>} - The site object.
|
||||
*/
|
||||
async getByDomainName(domainName) {
|
||||
const { data, error } = await supabase.from(SiteModel.tableName).select('*').eq('domain_name', domainName);
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching site by domain name:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = SiteModel;
|
||||
Reference in New Issue
Block a user