File size: 1,755 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Schema, model, Document, Types } from 'mongoose';

interface IFieldResponse {
  fieldId: string; // References field ID in Form
  value: any; // Can be string, number, array, etc.
  fileUrl?: string; // If file upload
}

export interface IFormResponse extends Document {
  form: Types.ObjectId;
  event?: Types.ObjectId; // If form is attached to event
  respondent: Types.ObjectId; // User who submitted
  
  // Response Data
  responses: IFieldResponse[];
  
  // Metadata
  submittedAt: Date;
  lastEditedAt?: Date;
  ipAddress?: string;
  userAgent?: string;
  
  // Status
  isComplete: boolean;
  
  createdAt: Date;
  updatedAt: Date;
}

const FormResponseSchema = new Schema<IFormResponse>({
  form: {
    type: Schema.Types.ObjectId,
    ref: 'Form',
    required: true,
    index: true
  },
  
  event: {
    type: Schema.Types.ObjectId,
    ref: 'Event',
    index: true
  },
  
  respondent: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    index: true
  },
  
  responses: [{
    fieldId: {
      type: String,
      required: true
    },
    value: {
      type: Schema.Types.Mixed,
      required: true
    },
    fileUrl: String
  }],
  
  submittedAt: {
    type: Date,
    default: Date.now,
    index: true
  },
  
  lastEditedAt: Date,
  
  ipAddress: String,
  
  userAgent: String,
  
  isComplete: {
    type: Boolean,
    default: true
  }
}, {
  timestamps: true
});

// Compound index: one response per user per form (if oneResponsePerUser is true)
FormResponseSchema.index({ form: 1, respondent: 1 }, { unique: true });

// Index for querying responses by event
FormResponseSchema.index({ event: 1, submittedAt: -1 });

export default model<IFormResponse>('FormResponse', FormResponseSchema);