const mongoose = require('mongoose');
const optionsSchema = new mongoose.Schema({
value: String,
label: String,
});
const customFieldSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
type: {
type: String,
enum: [
'text',
'number',
'dropdown',
'multiSelect',
'singleSelect',
'currency',
'date',
],
required: true,
},
},
{
timestamps: true,
},
);
const CustomField = mongoose.model('CustomField', customFieldSchema);
const formCustomFieldSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
customField: {
type: mongoose.Schema.Types.ObjectId,
ref: 'CustomField',
required: true,
},
form: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Form',
required: true,
},
stringValue: {
type: String,
default: undefined,
},
numberValue: {
type: Number,
default: undefined,
},
dateValue: {
type: Date,
default: undefined,
},
arrayValue: {
type: [optionsSchema],
default: undefined,
required: function () {
return ['dropdown', 'singleSelect', 'multiSelect'].includes(this.type);
},
},
mode: {
type: String,
enum: ['create', 'edit'],
},
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Member',
required: true,
},
},
{
timestamps: true,
},
);
formCustomFieldSchema.index({ customField: 1, form: 1 }, { unique: true });
const FormCustomField = mongoose.model(
'FormCustomField',
formCustomFieldSchema,
);
module.exports = {
CustomField,
FormCustomField,
};