aboutsummaryrefslogtreecommitdiff
path: root/models/votes
diff options
context:
space:
mode:
Diffstat (limited to 'models/votes')
-rw-r--r--models/votes/vote.model.ts7
-rw-r--r--models/votes/vote.schema.ts25
2 files changed, 32 insertions, 0 deletions
diff --git a/models/votes/vote.model.ts b/models/votes/vote.model.ts
new file mode 100644
index 0000000..df2307e
--- /dev/null
+++ b/models/votes/vote.model.ts
@@ -0,0 +1,7 @@
+import { Model, model } from 'mongoose';
+import { VoteSchema, voteSchema } from './vote.schema';
+
+voteSchema.index({ pollId: 1, userId: 1 }, { unique: true }); // Unique together
+
+export default model<VoteSchema, Model<VoteSchema>>('Vote', voteSchema);
+
diff --git a/models/votes/vote.schema.ts b/models/votes/vote.schema.ts
new file mode 100644
index 0000000..63ba212
--- /dev/null
+++ b/models/votes/vote.schema.ts
@@ -0,0 +1,25 @@
+import { Document, Schema, Types } from 'mongoose';
+import { Vote } from 'which-types';
+
+export interface VoteSchema extends Document, Omit<Vote, '_id'> {
+ password: string;
+}
+
+export const voteSchema = new Schema({
+ userId: {
+ type: Types.ObjectId,
+ ref: 'user',
+ required: true
+ },
+ pollId: {
+ type: Types.ObjectId,
+ ref: 'poll',
+ required: true
+ },
+ which: {
+ type: String,
+ enum: ['left', 'right'],
+ required: true
+ }
+}, { timestamps: true });
+