2024-03-27 00:38:32 +01:00
|
|
|
const { safeJsonParse } = require("../utils/http");
|
2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-09-12 01:27:04 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
const Invite = {
|
|
|
|
makeCode: () => {
|
|
|
|
const uuidAPIKey = require("uuid-apikey");
|
|
|
|
return uuidAPIKey.create().apiKey;
|
|
|
|
},
|
|
|
|
|
2024-03-27 00:38:32 +01:00
|
|
|
create: async function ({ createdByUserId = 0, workspaceIds = [] }) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const invite = await prisma.invites.create({
|
|
|
|
data: {
|
|
|
|
code: this.makeCode(),
|
|
|
|
createdBy: createdByUserId,
|
2024-03-27 00:38:32 +01:00
|
|
|
workspaceIds: JSON.stringify(workspaceIds),
|
2023-09-28 23:00:03 +02:00
|
|
|
},
|
2023-07-25 19:37:04 +02:00
|
|
|
});
|
2023-09-28 23:00:03 +02:00
|
|
|
return { invite, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO CREATE INVITE.", error.message);
|
|
|
|
return { invite: null, error: error.message };
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
deactivate: async function (inviteId = null) {
|
|
|
|
try {
|
2024-03-27 00:38:32 +01:00
|
|
|
await prisma.invites.update({
|
2023-09-28 23:00:03 +02:00
|
|
|
where: { id: Number(inviteId) },
|
|
|
|
data: { status: "disabled" },
|
2023-07-25 19:37:04 +02:00
|
|
|
});
|
2023-09-28 23:00:03 +02:00
|
|
|
return { success: true, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { success: false, error: error.message };
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
markClaimed: async function (inviteId = null, user) {
|
|
|
|
try {
|
|
|
|
const invite = await prisma.invites.update({
|
|
|
|
where: { id: Number(inviteId) },
|
|
|
|
data: { status: "claimed", claimedBy: user.id },
|
2023-07-25 19:37:04 +02:00
|
|
|
});
|
2024-03-27 00:38:32 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (!!invite?.workspaceIds) {
|
|
|
|
const { Workspace } = require("./workspace");
|
|
|
|
const { WorkspaceUser } = require("./workspaceUsers");
|
|
|
|
const workspaceIds = (await Workspace.where({})).map(
|
|
|
|
(workspace) => workspace.id
|
|
|
|
);
|
|
|
|
const ids = safeJsonParse(invite.workspaceIds)
|
|
|
|
.map((id) => Number(id))
|
|
|
|
.filter((id) => workspaceIds.includes(id));
|
|
|
|
if (ids.length !== 0) await WorkspaceUser.createMany(user.id, ids);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(
|
|
|
|
"Could not add user to workspaces automatically",
|
|
|
|
e.message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
return { success: true, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { success: false, error: error.message };
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
get: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const invite = await prisma.invites.findFirst({ where: clause });
|
|
|
|
return invite || 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
|
|
|
count: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const count = await prisma.invites.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.invites.deleteMany({ where: clause });
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
where: async function (clause = {}, limit) {
|
|
|
|
try {
|
|
|
|
const invites = await prisma.invites.findMany({
|
|
|
|
where: clause,
|
|
|
|
take: limit || undefined,
|
|
|
|
});
|
|
|
|
return invites;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
whereWithUsers: async function (clause = {}, limit) {
|
|
|
|
const { User } = require("./user");
|
|
|
|
try {
|
|
|
|
const invites = await this.where(clause, limit);
|
|
|
|
for (const invite of invites) {
|
|
|
|
if (invite.claimedBy) {
|
|
|
|
const acceptedUser = await User.get({ id: invite.claimedBy });
|
|
|
|
invite.claimedBy = {
|
|
|
|
id: acceptedUser?.id,
|
|
|
|
username: acceptedUser?.username,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invite.createdBy) {
|
|
|
|
const createdUser = await User.get({ id: invite.createdBy });
|
|
|
|
invite.createdBy = {
|
|
|
|
id: createdUser?.id,
|
|
|
|
username: createdUser?.username,
|
|
|
|
};
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
2023-09-28 23:00:03 +02:00
|
|
|
return invites;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
module.exports = { Invite };
|