implement endpoints for list, create & remove of notification methods; move owner check of notification method into own file

This commit is contained in:
Michi 2025-10-21 21:41:21 +02:00
parent 60a75f7894
commit 0b11cccf33
8 changed files with 179 additions and 50 deletions

View 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();
}
}