Site settings and fires fromNow Reactive
This commit is contained in:
parent
e4b0135953
commit
379e72a9e7
15 changed files with 482 additions and 64 deletions
49
imports/api/SiteSettings/SiteSettings.js
Normal file
49
imports/api/SiteSettings/SiteSettings.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* eslint-disable consistent-return */
|
||||
/* eslint-disable import/no-absolute-path */
|
||||
|
||||
import { Mongo } from 'meteor/mongo';
|
||||
import SimpleSchema from 'simpl-schema';
|
||||
import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js';
|
||||
import SiteSettingsTypes from './SiteSettingsTypes';
|
||||
|
||||
const SiteSettings = new Mongo.Collection('siteSettings', { idGeneration: 'MONGO' });
|
||||
|
||||
SiteSettings.allow({
|
||||
insert: () => false,
|
||||
update: () => false,
|
||||
remove: () => false
|
||||
});
|
||||
|
||||
SiteSettings.deny({
|
||||
insert: () => true,
|
||||
update: () => true,
|
||||
remove: () => true
|
||||
});
|
||||
|
||||
SiteSettings.get = (name) => {
|
||||
const setting = SiteSettings.findOne({
|
||||
name
|
||||
});
|
||||
return typeof setting === 'object' ? setting.value : undefined;
|
||||
};
|
||||
|
||||
SiteSettings.observe = (name, callback) => {
|
||||
SiteSettings.find({ name }).observe({
|
||||
added: (document) => {
|
||||
callback(document.value);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SiteSettings.getSchema = type => new SimpleSchema({
|
||||
name: String,
|
||||
type: String,
|
||||
description: String,
|
||||
value: SiteSettingsTypes[type].value,
|
||||
createdAt: defaultCreatedAt,
|
||||
updatedAt: defaultUpdateAt
|
||||
});
|
||||
|
||||
// SiteSettings.attachSchema(SiteSettings.schema);
|
||||
|
||||
export default SiteSettings;
|
||||
66
imports/api/SiteSettings/SiteSettingsTypes.js
Normal file
66
imports/api/SiteSettings/SiteSettingsTypes.js
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// Adapted from: https://github.com/yogiben/meteor-admin-settings
|
||||
|
||||
const SiteSettingsTypes = {
|
||||
string: {
|
||||
value: {
|
||||
type: String
|
||||
}
|
||||
},
|
||||
textarea: {
|
||||
value: {
|
||||
type: String
|
||||
/* autoform: {
|
||||
* afFieldInput: {
|
||||
* rows: 5
|
||||
* }
|
||||
* } */
|
||||
}
|
||||
},
|
||||
number: {
|
||||
value: {
|
||||
type: Number
|
||||
}
|
||||
},
|
||||
boolean: {
|
||||
value: {
|
||||
type: Boolean
|
||||
/* autoform: {
|
||||
* afFieldInput: {
|
||||
* type: 'boolean-select'
|
||||
* }
|
||||
* } */
|
||||
}
|
||||
},
|
||||
date: {
|
||||
value: {
|
||||
type: Date
|
||||
/* autoform: {
|
||||
* afFieldInput: {
|
||||
* type: 'date'
|
||||
* }
|
||||
* } */
|
||||
}
|
||||
},
|
||||
color: {
|
||||
value: {
|
||||
type: String
|
||||
/* autoform: {
|
||||
* afFieldInput: {
|
||||
* type: 'color'
|
||||
* }
|
||||
* } */
|
||||
}
|
||||
},
|
||||
password: {
|
||||
value: {
|
||||
type: String
|
||||
/* autoform: {
|
||||
* afFieldInput: {
|
||||
* type: 'password'
|
||||
* }
|
||||
* } */
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default SiteSettingsTypes;
|
||||
60
imports/api/SiteSettings/methods.js
Normal file
60
imports/api/SiteSettings/methods.js
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { Meteor } from 'meteor/meteor';
|
||||
import { check } from 'meteor/check';
|
||||
import SiteSettings from './SiteSettings';
|
||||
import SiteSettingsTypes from './SiteSettingsTypes';
|
||||
import rateLimit from '../../modules/rate-limit';
|
||||
|
||||
Meteor.methods({
|
||||
'siteSettings.insert': function siteSettingsInsert(setting) {
|
||||
check(setting, {
|
||||
name: String,
|
||||
type: String,
|
||||
description: String,
|
||||
value: SiteSettingsTypes[self.type].value
|
||||
});
|
||||
|
||||
SiteSettings.getSchema.validate(setting);
|
||||
|
||||
try {
|
||||
return SiteSettings.insert({ owner: this.userId, ...setting });
|
||||
} catch (exception) {
|
||||
throw new Meteor.Error('500', exception);
|
||||
}
|
||||
},
|
||||
'siteSettings.update': function siteSettingsUpdate(setting) {
|
||||
check(setting, {
|
||||
_id: String,
|
||||
name: String,
|
||||
type: String,
|
||||
description: String,
|
||||
value: SiteSettingsTypes[self.type].value
|
||||
});
|
||||
|
||||
try {
|
||||
const siteSettingId = setting._id;
|
||||
SiteSettings.update(siteSettingId, { $set: setting });
|
||||
return siteSettingId; // Return _id so we can redirect to siteSetting after update.
|
||||
} catch (exception) {
|
||||
throw new Meteor.Error('500', exception);
|
||||
}
|
||||
},
|
||||
'siteSettings.remove': function siteSettingsRemove(siteSettingId) {
|
||||
check(siteSettingId, String);
|
||||
|
||||
try {
|
||||
return SiteSettings.remove(siteSettingId);
|
||||
} catch (exception) {
|
||||
throw new Meteor.Error('500', exception);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rateLimit({
|
||||
methods: [
|
||||
'siteSettings.insert',
|
||||
'siteSettings.update',
|
||||
'siteSettings.remove'
|
||||
],
|
||||
limit: 5,
|
||||
timeRange: 1000
|
||||
});
|
||||
6
imports/api/SiteSettings/server/publications.js
Normal file
6
imports/api/SiteSettings/server/publications.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/* eslint-disable prefer-arrow-callback */
|
||||
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import SiteSettings from '../SiteSettings';
|
||||
|
||||
Meteor.publish('settings', () => SiteSettings.find());
|
||||
Loading…
Add table
Add a link
Reference in a new issue