Skip to content

Commit

Permalink
Filter actions by role, submit actions
Browse files Browse the repository at this point in the history
  • Loading branch information
FyreByrd committed Sep 18, 2024
1 parent f449020 commit 452278b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { NoAdminS3 } from 'sil.appbuilder.portal.common';
import { createActor, type Snapshot } from 'xstate';
import { redirect } from '@sveltejs/kit';
import { filterObject } from '$lib/filterObject';
import { fail, superValidate } from 'sveltekit-superforms';
import { valibot } from 'sveltekit-superforms/adapters';
import * as v from 'valibot';
import { RoleId } from 'sil.appbuilder.portal.common/prisma';

const sendActionSchema = v.object({
state: v.string(),
action: v.string(),
comment: v.string()
});

type Fields = {
ownerName?: string; //Product.Project.Owner.Name
Expand All @@ -19,6 +29,7 @@ type Fields = {
};

export const load = (async ({ params, url, locals }) => {
const session = await locals.auth();
// TODO: permission check
const actor = createActor(NoAdminS3, {
snapshot: await getSnapshot(params.product_id, NoAdminS3),
Expand All @@ -40,6 +51,7 @@ export const load = (async ({ params, url, locals }) => {
Language: true,
Owner: {
select: {
Id: true,
Name: true,
Email: true
}
Expand All @@ -53,7 +65,21 @@ export const load = (async ({ params, url, locals }) => {
Email: true
}
}
: undefined
: undefined,
Authors: {
select: {
UserId: true
}
},
Organization: {
select: {
UserRoles: {
where: {
RoleId: RoleId.OrgAdmin
}
}
}
}
}
},
Store: {
Expand Down Expand Up @@ -105,8 +131,19 @@ export const load = (async ({ params, url, locals }) => {
const fields = snap.context.includeFields;

return {
//later: filter available actions by user role
actions: Object.keys(NoAdminS3.getStateNodeById(`${NoAdminS3.id}.${snap.value}`).on),
actions: Object.keys(NoAdminS3.getStateNodeById(`${NoAdminS3.id}.${snap.value}`).on).filter((a) => {
if (session?.user.userId === undefined) return false;
switch(a.split(":")[1]) {
case 'Owner':
return session.user.userId === product?.Project.Owner.Id;
case 'Author':
return product?.Project.Authors.map((a) => a.UserId).includes(session.user.userId);
case 'Admin':
return product?.Project.Organization.UserRoles.map((u) => u.UserId).includes(session.user.userId);
default:
return false;
}
}).map((a) => a.split(":")),
taskTitle: snap.value,
instructions: snap.context.instructions,
//filter fields/files/reviewers based on task once workflows are implemented
Expand Down Expand Up @@ -136,14 +173,25 @@ export const load = (async ({ params, url, locals }) => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request }) => {
default: async ({ request, params }) => {
// TODO: permission check
const data = await request.formData();
const form = await superValidate(request, valibot(sendActionSchema));
if (!form.valid) return fail(400, { form, ok: false });

//double check that state matches current snapshot
const snap = await getSnapshot(params.product_id, NoAdminS3);

const actor = createActor(NoAdminS3, {
snapshot: snap,
input: {}
});

console.log(data.get('action'));
console.log(data.get('comment'));
actor.start();

//double check that state matches current snapshot
if (form.data.state === actor.getSnapshot().value) {
const action = Object.keys(NoAdminS3.getStateNodeById(`${NoAdminS3.id}.${actor.getSnapshot().value}`).on).filter((a) => a.split(":")[0] === form.data.action);
actor.send({ type: action[0], comment: form.data.comment });
}

redirect(302, '/tasks');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{#if data.actions?.length}
<div class="flex flex-row gap-x-3">
{#each data.actions as action}
<input type="submit" name="action" class="btn" value={action}>
<input type="submit" name="action" class="btn" value={action[0]}>
{/each}
</div>
{/if}
Expand All @@ -21,6 +21,7 @@
</div>
<textarea class="textarea textarea-bordered h-24" name="comment"></textarea>
</label>
<input type="hidden" name="state" value={data.taskTitle}>
</form>
<hr class="border-t-4 my-2">
<h2>
Expand Down

0 comments on commit 452278b

Please sign in to comment.