diff --git a/src/lib/utils/services/notification/note.txt b/src/lib/utils/services/notification/note.txt new file mode 100644 index 00000000..1da9b93f --- /dev/null +++ b/src/lib/utils/services/notification/note.txt @@ -0,0 +1,4 @@ +1. Welcome new user into classroomio +2. Sends teacher welcome to course +3. Notify teacher someone wants to buy course +4 diff --git a/src/lib/utils/services/notification/notification.ts b/src/lib/utils/services/notification/notification.ts new file mode 100644 index 00000000..87d678f6 --- /dev/null +++ b/src/lib/utils/services/notification/notification.ts @@ -0,0 +1,41 @@ +import { supabase } from '$lib/utils/functions/supabase'; + +export class Notification { + name = ''; + accessToken = ''; + + constructor(name: string) { + this.name = name; + } + + async getAccessToken() { + const { data } = await supabase.auth.getSession(); + this.accessToken = data.session?.access_token || ''; + + return this.accessToken; + } + + async send(path: string, body: { [k: string]: string }) { + try { + await this.getAccessToken(); + const response = await fetch(path, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: this.accessToken + }, + body: JSON.stringify(body) + }); + if (!response.ok) { + throw new Error(await response.text()); + } + const result = await response.json(); + + console.log(`${this.name} sent email result`, result); + } catch (error) { + console.log(`${this.name} sent email error`, error); + } + } +} + +export class InviteNotification extends Notification {}