2022-12-13 07:37:49 -05:00
|
|
|
import { db, ObjectId } from '../../mongodb.js'
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2024-06-10 04:39:59 -04:00
|
|
|
export class MissingThreadError extends Error {}
|
|
|
|
|
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
|
2023-10-17 08:48:51 -04:00
|
|
|
projectId = new ObjectId(projectId.toString())
|
2022-01-07 06:48:39 -05:00
|
|
|
if (threadId !== GLOBAL_THREAD) {
|
2023-10-17 08:48:51 -04:00
|
|
|
threadId = new ObjectId(threadId.toString())
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
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
|
|
|
)
|
2023-10-17 08:48:51 -04:00
|
|
|
return result
|
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) {
|
2024-05-22 05:37:08 -04:00
|
|
|
return await db.rooms
|
2022-01-07 06:48:39 -05:00
|
|
|
.find(
|
2020-08-27 11:31:27 -04:00
|
|
|
{
|
2023-10-17 08:48:51 -04:00
|
|
|
project_id: new 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) {
|
2024-05-22 05:37:08 -04:00
|
|
|
return await db.rooms
|
2022-03-16 08:20:52 -04:00
|
|
|
.find(
|
|
|
|
{
|
2023-10-17 08:48:51 -04:00
|
|
|
project_id: new ObjectId(projectId.toString()),
|
2022-03-16 08:20:52 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2023-10-17 08:48:51 -04:00
|
|
|
project_id: new ObjectId(projectId.toString()),
|
|
|
|
thread_id: new ObjectId(threadId.toString()),
|
2022-01-07 06:48:39 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$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
|
|
|
{
|
2023-10-17 08:48:51 -04:00
|
|
|
project_id: new ObjectId(projectId.toString()),
|
|
|
|
thread_id: new ObjectId(threadId.toString()),
|
2022-01-07 06:48:39 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$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({
|
2023-10-17 08:48:51 -04:00
|
|
|
project_id: new ObjectId(projectId.toString()),
|
2022-03-16 08:20:52 -04:00
|
|
|
})
|
|
|
|
}
|
2024-05-21 11:50:40 -04:00
|
|
|
|
|
|
|
export async function getResolvedThreadIds(projectId) {
|
|
|
|
const resolvedThreadIds = await db.rooms
|
|
|
|
.find(
|
|
|
|
{
|
|
|
|
project_id: new ObjectId(projectId),
|
|
|
|
thread_id: { $exists: true },
|
|
|
|
resolved: { $exists: true },
|
|
|
|
},
|
|
|
|
{ projection: { thread_id: 1 } }
|
|
|
|
)
|
|
|
|
.map(record => record.thread_id.toString())
|
|
|
|
.toArray()
|
|
|
|
return resolvedThreadIds
|
|
|
|
}
|
2024-06-10 04:39:59 -04:00
|
|
|
|
|
|
|
export async function duplicateThread(projectId, threadId) {
|
|
|
|
const room = await db.rooms.findOne({
|
|
|
|
project_id: new ObjectId(projectId),
|
|
|
|
thread_id: new ObjectId(threadId),
|
|
|
|
})
|
|
|
|
if (!room) {
|
|
|
|
throw new MissingThreadError('Trying to duplicate a non-existent thread')
|
|
|
|
}
|
|
|
|
const newRoom = {
|
|
|
|
project_id: room.project_id,
|
|
|
|
thread_id: new ObjectId(),
|
|
|
|
}
|
|
|
|
if (room.resolved) {
|
|
|
|
newRoom.resolved = room.resolved
|
|
|
|
}
|
|
|
|
const confirmation = await db.rooms.insertOne(newRoom)
|
|
|
|
newRoom._id = confirmation.insertedId
|
|
|
|
return { oldRoom: room, newRoom }
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function findThreadsById(projectId, threadIds) {
|
|
|
|
return await db.rooms
|
|
|
|
.find({
|
|
|
|
project_id: new ObjectId(projectId),
|
|
|
|
thread_id: { $in: threadIds.map(id => new ObjectId(id)) },
|
|
|
|
})
|
|
|
|
.toArray()
|
|
|
|
}
|