mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
## Description Adds user management capabilities to our current API. Allows for adding, removing, listing and updating members of a given team using a valid API token. ## Related Issue N/A ## Changes Made - Added an endpoint for inviting a team member - Added an endpoint for removing a team member - Added an endpoint for updating a team member - Added an endpoint for listing team members ## Testing Performed Tests were written for this feature request
47 lines
939 B
TypeScript
47 lines
939 B
TypeScript
import { prisma } from '@documenso/prisma';
|
|
|
|
import { hashString } from '../auth/hash';
|
|
|
|
export const getApiTokenByToken = async ({ token }: { token: string }) => {
|
|
const hashedToken = hashString(token);
|
|
|
|
const apiToken = await prisma.apiToken.findFirst({
|
|
where: {
|
|
token: hashedToken,
|
|
},
|
|
include: {
|
|
team: true,
|
|
user: true,
|
|
},
|
|
});
|
|
|
|
if (!apiToken) {
|
|
throw new Error('Invalid token');
|
|
}
|
|
|
|
if (apiToken.expires && apiToken.expires < new Date()) {
|
|
throw new Error('Expired token');
|
|
}
|
|
|
|
// Handle a silly choice from many moons ago
|
|
if (apiToken.team && !apiToken.user) {
|
|
apiToken.user = await prisma.user.findFirst({
|
|
where: {
|
|
id: apiToken.team.ownerUserId,
|
|
},
|
|
});
|
|
}
|
|
|
|
const { user } = apiToken;
|
|
|
|
// This will never happen but we need to narrow types
|
|
if (!user) {
|
|
throw new Error('Invalid token');
|
|
}
|
|
|
|
return {
|
|
...apiToken,
|
|
user,
|
|
};
|
|
};
|