56 lines
2.0 KiB
JavaScript
56 lines
2.0 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const mongoosePaginate = require('mongoose-paginate-v2');
|
|
const s3Storage = require('../plugins/s3Storage');
|
|
|
|
function image(img) {
|
|
if (!img) return null;
|
|
if (img.startsWith('http://') || img.startsWith('https://')) return img;
|
|
return s3Storage.getPublicUrl(`uploads/images/election/${img}`);
|
|
}
|
|
|
|
const CandidatesSchema = mongoose.Schema({
|
|
firstName: String,
|
|
lastName: String,
|
|
bornDate : {type: Date , default: null},
|
|
education: {type: String, default: null},
|
|
nationalCode : {type: String, default: null},
|
|
resume : {type: String, default: null},
|
|
profilePic : {type: String, get: image, default: null},
|
|
_creator :{type: mongoose.Schema.Types.ObjectId, ref: 'User'},
|
|
_updatedBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
|
|
});
|
|
|
|
const VotersSchema = mongoose.Schema({
|
|
firstName: String,
|
|
lastName: String,
|
|
nationalCode : String,
|
|
password: String,
|
|
scope:{type: Array, default: ['voter']},
|
|
username: {type: String, unique: true},
|
|
mobileNumber: {type: String, default: null},
|
|
job: {type: String, default: null},
|
|
voted: {type: Boolean, default: false},
|
|
_creator :{type: mongoose.Schema.Types.ObjectId, ref: 'User'},
|
|
_updatedBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
|
|
});
|
|
|
|
const ElectionSchema = mongoose.Schema({
|
|
title: String,
|
|
description : {type : String , default : null},
|
|
catId: {type: mongoose.Schema.Types.ObjectId, ref: 'Category'},
|
|
allCat: [{type: mongoose.Schema.Types.ObjectId, ref: 'Category'}],
|
|
rootParent: {type: mongoose.Schema.Types.ObjectId, ref: 'Category'},
|
|
candidates : [CandidatesSchema],
|
|
voters : [VotersSchema],
|
|
votes : {type : Array , default : []},
|
|
startDate : Date,
|
|
endDate : Date,
|
|
publish: {type: Boolean, default: false},
|
|
_creator :{type: mongoose.Schema.Types.ObjectId, ref: 'User'},
|
|
_updatedBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
|
|
});
|
|
|
|
ElectionSchema.plugin(mongoosePaginate);
|
|
|
|
module.exports = mongoose.model('Election', ElectionSchema);
|