-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
66 lines (52 loc) · 1.68 KB
/
database.py
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
from datetime import datetime, UTC
import bson
from bson import ObjectId
from motor.motor_asyncio import AsyncIOMotorClient
from config import config
client = AsyncIOMotorClient(config.MONGO_URI)
ticketDB = client[config.MONGO_DB_NAME]
async def createTicket(userID, ticketText, ticketID, ticketMessageID):
ticket = {
"_id": ticketID,
"TicketStatus": 1,
"Date": datetime.now(UTC),
"TicketText": ticketText,
"TicketRating": None,
"TicketMessageID": ticketMessageID,
"TelegramUserID": userID
}
await ticketDB.tickets.insert_one(ticket)
async def updateTicketStatus(ticketID, status):
await ticketDB.tickets.update_one(
{"_id": ObjectId(ticketID)},
{"$set": {"TicketStatus": status}}
)
async def getTicketByID(ticketID):
ticket = await ticketDB.tickets.find_one(
{"_id": ObjectId(ticketID)}
)
return ticket
async def setTicketRating(ticketID, rating):
await ticketDB.tickets.update_one(
{"_id": ObjectId(ticketID)},
{"$set": {"TicketRating": rating}}
)
async def initUser(userID):
userInfo = {
"TelegramUserID": userID,
"ClosedTickets": [],
}
await ticketDB.user.insert_one(userInfo)
async def getUser(userID):
user = await ticketDB.user.find_one(
{"TelegramUserID": userID}
)
return user
async def closeTicket(userID, ticketID):
user = await getUser(userID)
tickets = user['ClosedTickets']
tickets.append(bson.ObjectId(ticketID))
await ticketDB.user.update_one(
{"TelegramUserID": userID},
{"$set":{"ClosedTickets": tickets}}
)