diff --git a/my-app/src/lib/server/api.ts b/my-app/src/lib/server/api.ts index 58d6a0b..10a65c0 100644 --- a/my-app/src/lib/server/api.ts +++ b/my-app/src/lib/server/api.ts @@ -31,7 +31,7 @@ async function send(method: Method, { path, data, headers = {} }: Request) { return text ? JSON.parse(text) : {}; } - throw error(res.status); + throw error(res.status, await res.text()); } function get(path: string, headers?: Headers) { @@ -92,3 +92,9 @@ export const categoryService = { get: (id: Id, cookies: Cookies) => get(`category/${id}`, getAuthHeader(cookies)), list: (groupId: Id, cookies: Cookies) => get(`category/${groupId}`, getAuthHeader(cookies)) }; +export const inviteService = { + get: (token: string, cookies: Cookies) => get(`invite/${token}`, getAuthHeader(cookies)), + send: (data: SendInvite, cookies: Cookies) => post(`invite`, data, getAuthHeader(cookies)), + accept: (data: AcceptInvite, cookies: Cookies) => + post(`invite/join/${data.token}`, undefined!, getAuthHeader(cookies)) +}; diff --git a/my-app/src/lib/svgs.ts b/my-app/src/lib/svgs.ts index d4f46cb..7886c19 100644 --- a/my-app/src/lib/svgs.ts +++ b/my-app/src/lib/svgs.ts @@ -4,3 +4,6 @@ export const PENCIL_SVG = export const INFO_SVG = ''; + +export const ADD_USER_SVG = + ''; diff --git a/my-app/src/routes/groups/+page.svelte b/my-app/src/routes/groups/+page.svelte index c9b7d19..be50c8d 100644 --- a/my-app/src/routes/groups/+page.svelte +++ b/my-app/src/routes/groups/+page.svelte @@ -50,6 +50,7 @@ Editar grupo + Miembros diff --git a/my-app/src/routes/groups/members/[[id=integer]]/+page.server.ts b/my-app/src/routes/groups/members/[[id=integer]]/+page.server.ts new file mode 100644 index 0000000..905cd17 --- /dev/null +++ b/my-app/src/routes/groups/members/[[id=integer]]/+page.server.ts @@ -0,0 +1,9 @@ +import { groupService } from '$lib/server/api'; +import type { PageServerLoad } from './$types'; + +export const load: PageServerLoad = async ({ params, cookies }) => { + const id = Number(params.id) || 0; + const group: Group = await groupService.get(id, cookies); + const members: User[] = id ? await groupService.listAllMembers(id, cookies) : []; + return { group, members }; +}; diff --git a/my-app/src/routes/groups/members/[[id=integer]]/+page.svelte b/my-app/src/routes/groups/members/[[id=integer]]/+page.svelte new file mode 100644 index 0000000..c42cd55 --- /dev/null +++ b/my-app/src/routes/groups/members/[[id=integer]]/+page.svelte @@ -0,0 +1,35 @@ + + + + {data.group.name} - {pageTitle} + + + + +
+

Miembros de {data.group.name}

+ {@html ADD_USER_SVG} +
+ +{#each data.members as user} +

{user.email}

+{/each} + + diff --git a/my-app/src/routes/invites/accept/[id]/+page.server.ts b/my-app/src/routes/invites/accept/[id]/+page.server.ts new file mode 100644 index 0000000..536c8f0 --- /dev/null +++ b/my-app/src/routes/invites/accept/[id]/+page.server.ts @@ -0,0 +1,30 @@ +import { inviteService } from '$lib/server/api'; +import { error, redirect } from '@sveltejs/kit'; +import type { Actions, PageServerLoad } from './$types'; + +export const load: PageServerLoad = async ({ params, cookies }) => { + const token = params.id || ''; + const invite: Invite = await inviteService.get(token, cookies); + return { invite }; +}; + +export const actions: Actions = { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + default: async ({ cookies, request, params }) => { + const token = params.id || ''; + + // TODO: El metodo getGroup esta restringido a solo lectura de usuarios que ya forman parte del team + // La idea era en el formulario mostrar el nombre y descripcion del grupo, pero por ahora estamos ok + // con esto. + + if (!token || !token.length) { + throw error(400, 'Token is required'); + } + + const invite: AcceptInvite = { + token: token + }; + await inviteService.accept(invite, cookies); + redirect(302, `/groups`); + } +}; diff --git a/my-app/src/routes/invites/accept/[id]/+page.svelte b/my-app/src/routes/invites/accept/[id]/+page.svelte new file mode 100644 index 0000000..87bd06a --- /dev/null +++ b/my-app/src/routes/invites/accept/[id]/+page.svelte @@ -0,0 +1,36 @@ + + + + {title} - {pageTitle} + + + + +

¡Has sido Invitad@ a un grupo!

+
+
+ + + + + +
+
diff --git a/my-app/src/routes/invites/send/[[id=integer]]/+page.server.ts b/my-app/src/routes/invites/send/[[id=integer]]/+page.server.ts new file mode 100644 index 0000000..ff25844 --- /dev/null +++ b/my-app/src/routes/invites/send/[[id=integer]]/+page.server.ts @@ -0,0 +1,30 @@ +import { groupService, inviteService } from '$lib/server/api'; +import { error, redirect } from '@sveltejs/kit'; +import type { Actions, PageServerLoad } from './$types'; +import { getUserId } from '$lib/auth'; + +export const load: PageServerLoad = async ({ params, cookies }) => { + const group_id = Number(params.id) || 0; + const group: Group = await groupService.get(group_id, cookies); + return { group }; +}; + +export const actions: Actions = { + default: async ({ cookies, request, params }) => { + const group_id = Number(params.id) || 0; + const data = await request.formData(); + const email = data.get('email')?.toString(); + + if (!email) { + throw error(400, 'Receiver Email is required'); + } + + const invite: SendInvite = { + sender_id: getUserId(cookies), + receiver_email: email, + group_id: group_id + }; + await inviteService.send(invite, cookies); + redirect(302, `/groups/members/${group_id}`); + } +}; diff --git a/my-app/src/routes/invites/send/[[id=integer]]/+page.svelte b/my-app/src/routes/invites/send/[[id=integer]]/+page.svelte new file mode 100644 index 0000000..3312f9c --- /dev/null +++ b/my-app/src/routes/invites/send/[[id=integer]]/+page.svelte @@ -0,0 +1,30 @@ + + + + {data.group.name} - {pageTitle} + + + + +

Invitar Usuario a {data.group.name}

+
+
+ + + +
+
diff --git a/my-app/src/types.d.ts b/my-app/src/types.d.ts index 3c44dc0..fcc3b3a 100644 --- a/my-app/src/types.d.ts +++ b/my-app/src/types.d.ts @@ -35,6 +35,23 @@ declare global { amount: number; date: string; }; + type Invite = { + id: Id; + sender_id: Id; + receiver_id: Id; + group_id: Id; + token: string; + status: string; + creation_date: string; + }; + type SendInvite = { + sender_id: Id; + receiver_email: string; + group_id: Id; + }; + type AcceptInvite = { + token: string; + }; } export {};