mirror of
https://github.com/michivonah/themepark-assistant.git
synced 2025-12-22 22:16:29 +01:00
implement endpoints for list, create & remove of notification methods; move owner check of notification method into own file
This commit is contained in:
parent
60a75f7894
commit
0b11cccf33
8 changed files with 179 additions and 50 deletions
30
api/src/lib/check-notification-method-owner.ts
Normal file
30
api/src/lib/check-notification-method-owner.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { getDbContext, getDbEnv } from '../db/client'
|
||||
import { notificationMethod } from '../db/schema';
|
||||
import { NotificationMethodSelect } from '../types/notification-method'
|
||||
import { DatabaseError, InvalidParameter } from '../errors'
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
type NotificationMethodUser = Pick<NotificationMethodSelect, "userId">;
|
||||
|
||||
/**
|
||||
* Checks if a specified user is the owner of a notification method and returns the owner if valid
|
||||
* @param db DB connection (already defined as variable/const)
|
||||
* @param methodId notificationMethodId to check
|
||||
* @param userId User to check wheter it is the owner
|
||||
* @returns Object with the owners userId
|
||||
*/
|
||||
export async function getNotificationMethodOwner(db: ReturnType<typeof getDbContext | typeof getDbEnv>, methodId: number, userId: number): Promise<NotificationMethodUser>{
|
||||
try{
|
||||
const method = await db.select({
|
||||
userId: notificationMethod.userId
|
||||
}).from(notificationMethod)
|
||||
.where(eq(notificationMethod.id, methodId)).get();
|
||||
|
||||
if(!method || method.userId !== userId) throw new InvalidParameter('notificationMethodId');
|
||||
else return method;
|
||||
}
|
||||
catch(e){
|
||||
if(e instanceof InvalidParameter) throw e;
|
||||
throw new DatabaseError();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue