Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.version = await this.getNextVersion();
},
async afterDelete() {
// If the deleted form is the root form - delete its revisions
if (this.id === this.parent) {
// Delete all revisions.
const revisions = await Form.find({
query: { parent: this.parent }
});
return Promise.all(revisions.map(rev => rev.delete()));
}
}
}),
withProps({
get overallStats() {
return new Promise(async resolve => {
const plugin = context.plugins.byName("forms-resolver-overall-stats");
if (!plugin) {
throw Error(
`Resolver plugin "forms-resolver-overall-stats" is not configured!`
);
}
const stats = await plugin.resolve({ form: this, context });
if (!stats) {
return resolve({
submissions: 0,
views: 0,
),
firstName: string(),
lastName: string(),
roles: ref({
list: true,
instanceOf: [SecurityRole, "model"],
using: [SecurityRoles2Models, "role"]
}),
groups: ref({
list: true,
instanceOf: [SecurityGroup, "model"],
using: [SecurityGroups2Models, "group"]
}),
avatar: context.commodo.fields.id()
})),
withProps(instance => ({
__access: null,
get fullName() {
return `${instance.firstName} ${instance.lastName}`.trim();
},
get gravatar() {
return "https://www.gravatar.com/avatar/" + md5(instance.email);
},
get access() {
if (this.__access) {
return this.__access;
}
return new Promise(async resolve => {
const access = {
scopes: [],
roles: [],
instanceOf: withFields({
type: string({
validation: validation.create("required,in:error:warning:info:success"),
message: string(),
data: object(),
createdOn: date({ value: new Date() })
})
})()
})
})),
withHooks({
beforeCreate() {
this.meta.submittedOn = new Date();
}
}),
withProps({
addLog(log) {
if (!Array.isArray(this.logs)) {
this.logs = [];
}
this.logs = [...this.logs, log];
}
})
)(createBase());
};
export default context => baseFn => {
return flow(
withProps({
getUser() {
return context.user;
},
getUserId() {
return context.user ? context.user.id : null;
}
}),
withFields({
createdBy: skipOnPopulate()(id()),
updatedBy: skipOnPopulate()(id()),
deletedBy: skipOnPopulate()(id())
}),
withHooks({
beforeCreate() {
this.createdBy = this.getUserId();
},
const step = () =>
fields({
value: {},
instanceOf: flow(
withFields({
started: boolean({ value: false }),
startedOn: date(),
completed: boolean({ value: false }),
completedOn: date()
}),
withProps({
markAsStarted() {
this.started = true;
this.startedOn = new Date();
},
markAsCompleted() {
this.completed = true;
this.completedOn = new Date();
}
})
)()
});
instanceOf: withFields({
renderer: string({ value: "default" })
})()
}),
submitButtonLabel: i18nString({ context }),
successMessage: i18nObject({ context }),
termsOfServiceMessage: fields({
instanceOf: withFields({
message: i18nObject({ context }),
errorMessage: i18nString({ context }),
enabled: boolean()
})()
}),
reCaptcha: fields({
instanceOf: flow(
withProps({
settings: {
get enabled() {
return new Promise(async resolve => {
const settings = await FormSettings.load();
resolve(Boolean(get(settings, "data.reCaptcha.enabled")));
});
},
get siteKey() {
return new Promise(async resolve => {
const settings = await FormSettings.load();
resolve(get(settings, "data.reCaptcha.siteKey"));
});
},
get secretKey() {
return new Promise(async resolve => {
const settings = await FormSettings.load();
const publishedRev: PbPage = (await PbPage.findOne({
query: { published: true, parent: instance.parent }
}): any);
if (publishedRev) {
publishedRev.published = false;
await publishedRev.save();
}
});
}
return value;
})
)(boolean({ value: false })),
locked: skipOnPopulate()(boolean({ value: false }))
})),
withProps({
async getNextVersion() {
const revision = await PbPage.findOne({
query: { parent: this.parent, deleted: { $in: [true, false] } },
sort: { version: -1 }
});
if (!revision) {
return 1;
}
return revision.version + 1;
},
get isHomePage() {
return new Promise(async resolve => {
const settings = await PbSettings.load();
resolve(settings.data.pages.home === this.parent);
export default ({ createBase, context }) => {
const File = flow(
withName("File"),
withProps({
get src() {
const settings = context.files.getSettings();
return settings.data.srcPrefix + this.key;
}
}),
withFields({
key: setOnce()(string({ validation: validation.create("required,maxLength:200") })),
size: number(),
type: string({ validation: validation.create("maxLength:50") }),
name: string({ validation: validation.create("maxLength:100") }),
meta: object(),
tags: onSet(value => {
if (!Array.isArray(value)) {
return null;
}