File size: 1,537 Bytes
848090a
b63b62d
848090a
 
 
 
 
 
 
 
 
 
 
 
 
 
b63b62d
848090a
b63b62d
848090a
b63b62d
848090a
b63b62d
848090a
 
 
 
 
 
b63b62d
 
 
848090a
b63b62d
 
848090a
 
 
b63b62d
 
848090a
b63b62d
 
848090a
 
 
b63b62d
 
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
import os
import uuid

from pymongo import MongoClient
from datetime import datetime


class MongoDBManager:
    def __init__(self):
        mongodb_uri = str(os.getenv('MONGODB_URI'))
        db_name = str(os.getenv('DB_NAME'))
        collection_name = str(os.getenv('COLLECTION_NAME'))

        self.client = MongoClient(mongodb_uri)
        self.db = self.client[db_name]
        self.collection = self.db[collection_name]
        self.user_id = None

    def create_user_record(self):
        """ Create a new user record with empty answers. """
        self.user_id = str(uuid.uuid4())
        user_record = {
            "_id": self.user_id,  # using user_id as the unique identifier for the document
            "createdAt": datetime.now(),
            "questionnaireAnswers": {},
            "questionAnswers": {}
        }
        self.collection.insert_one(user_record)

    def update_question_answers(self, answers):
        """ Update question answers for a specific user. """
        self.create_user_record()
        update_result = self.collection.update_one(
            {"_id": self.user_id},
            {"$set": {"questionAnswers": answers}}
        )
        return update_result.modified_count

    def update_questionnaire_answers(self, answers):
        """ Update questionnaire answers for a specific user. """
        update_result = self.collection.update_one(
            {"_id": self.user_id},
            {"$set": {"questionnaireAnswers": answers}}
        )
        return update_result.modified_count