2022-12-13 07:37:49 -05:00
|
|
|
import { db, ObjectId } from '../../mongodb.js'
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const GLOBAL_THREAD = 'GLOBAL'
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function findOrCreateThread(projectId, threadId) {
|
2022-01-07 06:48:39 -05:00
|
|
|
let query, update
|
|
|
|
projectId = ObjectId(projectId.toString())
|
|
|
|
if (threadId !== GLOBAL_THREAD) {
|
|
|
|
threadId = ObjectId(threadId.toString())
|
|
|
|
}
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
if (threadId === GLOBAL_THREAD) {
|
|
|
|
query = {
|
|
|
|
project_id: projectId,
|
|
|
|
thread_id: { $exists: false },
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
update = {
|
|
|
|
project_id: projectId,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query = {
|
|
|
|
project_id: projectId,
|
|
|
|
thread_id: threadId,
|
|
|
|
}
|
|
|
|
update = {
|
|
|
|
project_id: projectId,
|
|
|
|
thread_id: threadId,
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2022-01-07 07:04:23 -05:00
|
|
|
const result = await db.rooms.findOneAndUpdate(
|
2022-01-07 06:48:39 -05:00
|
|
|
query,
|
|
|
|
{ $set: update },
|
2022-01-07 07:04:23 -05:00
|
|
|
{ upsert: true, returnDocument: 'after' }
|
2022-01-07 06:48:39 -05:00
|
|
|
)
|
2022-01-07 07:04:23 -05:00
|
|
|
return result.value
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-03-16 12:08:57 -04:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function findAllThreadRooms(projectId) {
|
2022-01-07 07:04:23 -05:00
|
|
|
return db.rooms
|
2022-01-07 06:48:39 -05:00
|
|
|
.find(
|
2020-08-27 11:31:27 -04:00
|
|
|
{
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: ObjectId(projectId.toString()),
|
2022-01-07 06:48:39 -05:00
|
|
|
thread_id: { $exists: true },
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
{
|
2022-01-07 06:48:39 -05:00
|
|
|
thread_id: 1,
|
|
|
|
resolved: 1,
|
|
|
|
}
|
|
|
|
)
|
2022-01-07 07:04:23 -05:00
|
|
|
.toArray()
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function findAllThreadRoomsAndGlobalThread(projectId) {
|
2022-03-16 08:20:52 -04:00
|
|
|
return db.rooms
|
|
|
|
.find(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
thread_id: 1,
|
|
|
|
resolved: 1,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.toArray()
|
|
|
|
}
|
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function resolveThread(projectId, threadId, userId) {
|
2022-01-07 07:04:23 -05:00
|
|
|
await db.rooms.updateOne(
|
2022-01-07 06:48:39 -05:00
|
|
|
{
|
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
thread_id: ObjectId(threadId.toString()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
resolved: {
|
|
|
|
user_id: userId,
|
|
|
|
ts: new Date(),
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
2022-01-07 07:04:23 -05:00
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
)
|
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function reopenThread(projectId, threadId) {
|
2022-01-07 07:04:23 -05:00
|
|
|
await db.rooms.updateOne(
|
2022-01-07 06:48:39 -05:00
|
|
|
{
|
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
thread_id: ObjectId(threadId.toString()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$unset: {
|
|
|
|
resolved: true,
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function deleteThread(projectId, threadId) {
|
2022-01-07 07:04:23 -05:00
|
|
|
const room = await findOrCreateThread(projectId, threadId)
|
|
|
|
await db.rooms.deleteOne({
|
|
|
|
_id: room._id,
|
2022-01-07 06:48:39 -05:00
|
|
|
})
|
2022-01-07 07:04:23 -05:00
|
|
|
return room._id
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function deleteAllThreadsInProject(projectId) {
|
2022-03-16 08:20:52 -04:00
|
|
|
await db.rooms.deleteMany({
|
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
})
|
|
|
|
}
|