2023-11-23 05:10:37 +01:00
|
|
|
import type { APIApplication } from "~types/entities/application";
|
|
|
|
|
import type { Application } from "@prisma/client";
|
2023-11-11 03:36:06 +01:00
|
|
|
import { client } from "~database/datasource";
|
2023-09-12 22:48:10 +02:00
|
|
|
|
2023-09-28 20:19:21 +02:00
|
|
|
/**
|
|
|
|
|
* Represents an application that can authenticate with the API.
|
2023-09-12 22:48:10 +02:00
|
|
|
*/
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Retrieves the application associated with the given access token.
|
|
|
|
|
* @param token The access token to retrieve the application for.
|
|
|
|
|
* @returns The application associated with the given access token, or null if no such application exists.
|
|
|
|
|
*/
|
|
|
|
|
export const getFromToken = async (
|
|
|
|
|
token: string
|
|
|
|
|
): Promise<Application | null> => {
|
|
|
|
|
const dbToken = await client.token.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
access_token: token,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
application: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return dbToken?.application || null;
|
|
|
|
|
};
|
2023-09-22 00:42:59 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Converts this application to an API application.
|
|
|
|
|
* @returns The API application representation of this application.
|
|
|
|
|
*/
|
2023-11-20 03:42:40 +01:00
|
|
|
export const applicationToAPI = (app: Application): APIApplication => {
|
2023-11-11 03:36:06 +01:00
|
|
|
return {
|
|
|
|
|
name: app.name,
|
|
|
|
|
website: app.website,
|
|
|
|
|
vapid_key: app.vapid_key,
|
|
|
|
|
};
|
|
|
|
|
};
|