2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-07-25 19:37:04 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
const WorkspaceUser = {
|
2023-07-25 19:37:04 +02:00
|
|
|
createMany: async function (userId, workspaceIds = []) {
|
|
|
|
if (workspaceIds.length === 0) return;
|
2023-07-27 03:06:53 +02:00
|
|
|
try {
|
2023-09-28 23:00:03 +02:00
|
|
|
await prisma.$transaction(
|
|
|
|
workspaceIds.map((workspaceId) =>
|
|
|
|
prisma.workspace_users.create({
|
|
|
|
data: { user_id: userId, workspace_id: workspaceId },
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
createManyUsers: async function (userIds = [], workspaceId) {
|
|
|
|
if (userIds.length === 0) return;
|
2023-07-27 03:06:53 +02:00
|
|
|
try {
|
2023-09-28 23:00:03 +02:00
|
|
|
await prisma.$transaction(
|
|
|
|
userIds.map((userId) =>
|
|
|
|
prisma.workspace_users.create({
|
|
|
|
data: {
|
|
|
|
user_id: Number(userId),
|
|
|
|
workspace_id: Number(workspaceId),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
create: async function (userId = 0, workspaceId = 0) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
await prisma.workspace_users.create({
|
|
|
|
data: { user_id: Number(userId), workspace_id: Number(workspaceId) },
|
2023-07-25 19:37:04 +02:00
|
|
|
});
|
2023-09-28 23:00:03 +02:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(
|
|
|
|
"FAILED TO CREATE WORKSPACE_USER RELATIONSHIP.",
|
|
|
|
error.message
|
|
|
|
);
|
2023-07-25 19:37:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
get: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const result = await prisma.workspace_users.findFirst({ where: clause });
|
|
|
|
return result || null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
where: async function (clause = {}, limit = null) {
|
|
|
|
try {
|
|
|
|
const results = await prisma.workspace_users.findMany({
|
|
|
|
where: clause,
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
count: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const count = await prisma.workspace_users.count({ where: clause });
|
|
|
|
return count;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
delete: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
await prisma.workspace_users.deleteMany({ where: clause });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.WorkspaceUser = WorkspaceUser;
|