anything-llm/server/models/invite.js
Sean Hatfield a126b5f5aa
Replace custom sqlite dbms with prisma (#239)
* WIP converted all sqlite models into prisma calls

* modify db setup and fix ApiKey model calls in admin.js

* renaming function params to be consistent

* converted adminEndpoints to utilize prisma orm

* converted chatEndpoints to utilize prisma orm

* converted inviteEndpoints to utilize prisma orm

* converted systemEndpoints to utilize prisma orm

* converted workspaceEndpoints to utilize prisma orm

* converting sql queries to prisma calls

* fixed default param bug for orderBy and limit

* fixed typo for workspace chats

* fixed order of deletion to account for sql relations

* fix invite CRUD and workspace management CRUD

* fixed CRUD for api keys

* created prisma setup scripts/docs for understanding how to use prisma

* prisma dependency change

* removing unneeded console.logs

* removing unneeded sql escape function

* linting and creating migration script

* migration from depreciated sqlite script update

* removing unneeded migrations in prisma folder

* create backup of old sqlite db and use transactions to ensure all operations complete successfully

* adding migrations to gitignore

* updated PRISMA.md docs for info on how to use sqlite migration script

* comment changes

* adding back migrations folder to repo

* Reviewing SQL and prisma integraiton on fresh repo

* update inline key replacement

* ensure migration script executes and maps foreign_keys regardless of db ordering

* run migration endpoint

* support new prisma backend

* bump version

* change migration call

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
2023-09-28 14:00:03 -07:00

123 lines
3.1 KiB
JavaScript

const prisma = require("../utils/prisma");
const Invite = {
makeCode: () => {
const uuidAPIKey = require("uuid-apikey");
return uuidAPIKey.create().apiKey;
},
create: async function (createdByUserId = 0) {
try {
const invite = await prisma.invites.create({
data: {
code: this.makeCode(),
createdBy: createdByUserId,
},
});
return { invite, error: null };
} catch (error) {
console.error("FAILED TO CREATE INVITE.", error.message);
return { invite: null, error: error.message };
}
},
deactivate: async function (inviteId = null) {
try {
const invite = await prisma.invites.update({
where: { id: Number(inviteId) },
data: { status: "disabled" },
});
return { success: true, error: null };
} catch (error) {
console.error(error.message);
return { success: false, error: error.message };
}
},
markClaimed: async function (inviteId = null, user) {
try {
const invite = await prisma.invites.update({
where: { id: Number(inviteId) },
data: { status: "claimed", claimedBy: user.id },
});
return { success: true, error: null };
} catch (error) {
console.error(error.message);
return { success: false, error: error.message };
}
},
get: async function (clause = {}) {
try {
const invite = await prisma.invites.findFirst({ where: clause });
return invite || null;
} catch (error) {
console.error(error.message);
return null;
}
},
count: async function (clause = {}) {
try {
const count = await prisma.invites.count({ where: clause });
return count;
} catch (error) {
console.error(error.message);
return 0;
}
},
delete: async function (clause = {}) {
try {
await prisma.invites.deleteMany({ where: clause });
return true;
} catch (error) {
console.error(error.message);
return false;
}
},
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 [];
}
},
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,
};
}
}
return invites;
} catch (error) {
console.error(error.message);
return [];
}
},
};
module.exports = { Invite };