Skip to content

Commit

Permalink
131 kunna ta bort access för specifika poster/personer (#350)
Browse files Browse the repository at this point in the history
* added temporary ban button, no functionality.

ban button but banning is not implemented

logic for banpolicy in database

working isBan checkbox

error color and good looking switch

correct colored switch

add adaptive text color for switch

add ban reason text field

textarea always visible

add information button

implement functional doorAccess banning

shift label for isBan button

add id to labels

remove id from labeled

* ban button but banning is not implemented

* logic for banpolicy in database

* working isBan checkbox

* error color and good looking switch

* correct colored switch

* add adaptive text color for switch

* add ban reason text field

* textarea always visible

* add information button

* implement functional doorAccess banning

* shift label for isBan button

* ban button but banning is not implemented

* logic for banpolicy in database

* working isBan checkbox

* error color and good looking switch

* correct colored switch

* add adaptive text color for switch

* add ban reason text field

* textarea always visible

* add information button

* implement functional doorAccess banning

* shift label for isBan button

* added temporary ban button, no functionality.

ban button but banning is not implemented

logic for banpolicy in database

working isBan checkbox

error color and good looking switch

correct colored switch

add adaptive text color for switch

add ban reason text field

textarea always visible

add information button

implement functional doorAccess banning

shift label for isBan button

add id to labels

remove id from labeled

* fix table appearance

* change svg icons to mdi

* fix isBan button formatting

* fix to information button horisontal alignment

* fix formatting remove message

* fix banaccess priority

* comment out old logic for banned users

* add prettier formatting

* add ban and information to DoorAccessPolicy

* Add db seed function to generate random position id

* add working door access ban

* fix

* final revisions

---------

Co-authored-by: jo4485li-s <[email protected]>
Co-authored-by: Daniel Adu-Gyan <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2024
1 parent 5f61da8 commit 36658d2
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "door_access_policies" ADD COLUMN "is_ban" BOOLEAN NOT NULL DEFAULT false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "door_access_policies" ADD COLUMN "information" VARCHAR(255);
2 changes: 2 additions & 0 deletions src/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ model DoorAccessPolicy {
member Member? @relation(fields: [studentId], references: [studentId], onDelete: NoAction, onUpdate: NoAction, map: "door_access_policies_member_id_foreign")
startDatetime DateTime? @map("start_datetime") @db.Timestamptz(6)
endDatetime DateTime? @map("end_datetime") @db.Timestamptz(6)
information String? @map("information") @db.VarChar(255)
isBan Boolean @map("is_ban") @default(false)
door Door @relation(fields: [doorName], references: [name], onDelete: NoAction, onUpdate: NoAction, map: "door_access_policies_door_name_foreign")
@@map("door_access_policies")
Expand Down
2 changes: 2 additions & 0 deletions src/database/schema.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ model DoorAccessPolicy {
member Member? @relation(fields: [studentId], references: [studentId], onDelete: NoAction, onUpdate: NoAction, map: "door_access_policies_member_id_foreign")
startDatetime DateTime? @map("start_datetime") @db.Timestamptz(6)
endDatetime DateTime? @map("end_datetime") @db.Timestamptz(6)
information String? @map("information") @db.VarChar(255)
isBan Boolean @map("is_ban") @default(false)
door Door @relation(fields: [doorName], references: [name], onDelete: NoAction, onUpdate: NoAction, map: "door_access_policies_door_name_foreign")

@@allow("create", has(auth().policies, "core:access:door:create"))
Expand Down
28 changes: 28 additions & 0 deletions src/database/seed/.snaplet/dataModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,34 @@
"isId": false,
"maxLength": null
},
{
"id": "public.door_access_policies.is_ban",
"name": "is_ban",
"columnName": "is_ban",
"type": "bool",
"isRequired": true,
"kind": "scalar",
"isList": false,
"isGenerated": false,
"sequence": false,
"hasDefaultValue": true,
"isId": false,
"maxLength": null
},
{
"id": "public.door_access_policies.information",
"name": "information",
"columnName": "information",
"type": "varchar",
"isRequired": false,
"kind": "scalar",
"isList": false,
"isGenerated": false,
"sequence": false,
"hasDefaultValue": false,
"isId": false,
"maxLength": 255
},
{
"name": "doors",
"type": "doors",
Expand Down
16 changes: 16 additions & 0 deletions src/database/seed/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { fakerSV as faker } from "@faker-js/faker";
import { type SeedClientOptions } from "@snaplet/seed";
import dayjs from "dayjs";
import apiNames from "$lib/utils/apiNames";
import * as positionEnums from "$lib/utils/committee-ordering/enums";

const getObjectValues = (obj: unknown): string[] => {
if (obj && typeof obj === "object") {
Expand Down Expand Up @@ -130,6 +131,14 @@ export const ACCESS_POLICIES = [
studentId: null,
}));

const getRandomPositionId = () => {
const positions = Object.entries(positionEnums).flatMap(([, positionEnum]) =>
Object.values(positionEnum).filter((val) => typeof val === "string"),
);
const position = faker.helpers.arrayElement(positions);
return position;
};

export const models: SeedClientOptions["models"] = {
member: {
data: {
Expand Down Expand Up @@ -210,6 +219,13 @@ export const models: SeedClientOptions["models"] = {
},
position: {
data: {
id: ({ store }) => {
let position = getRandomPositionId();
while (store.position.find((p) => p.id === position)) {
position = getRandomPositionId();
}
return position;
},
name: () => faker.person.jobTitle(),
nameEn: () => faker.person.jobTitle(),
description: () => faker.lorem.paragraph(),
Expand Down
27 changes: 6 additions & 21 deletions src/lib/utils/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,38 +87,23 @@ export const getCurrentDoorPoliciesForMember = async (
.findMany({
where: {
AND: [
{ isBan: false },
{
// is active, or indefinite
OR: [
{
startDatetime: null,
},
{
startDatetime: {
lte: new Date(),
},
},
{ startDatetime: null },
{ startDatetime: { lte: new Date() } },
],
},
{
// is active, or indefinite
OR: [
{
endDatetime: null,
},
{
endDatetime: {
gte: new Date(),
},
},
],
OR: [{ endDatetime: null }, { endDatetime: { gte: new Date() } }],
},
{
OR: [
{ studentId /* is for this user */ },
{
studentId, // is for this user
},
{
// or is for a role this user has
role: {
in: getDerivedRoles(
memberPositionIds.map((pos) => pos.id),
Expand Down
33 changes: 31 additions & 2 deletions src/routes/(app)/admin/doors/edit/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const load: PageServerLoad = async ({ locals, params }) => {
});
return {
doorAccessPolicies,
createForm: await superValidate(zod(createSchema)),
createForm: await superValidate(zod(createSchema), { id: "createForm" }),
banForm: await superValidate(zod(createSchema), { id: "banForm" }),
deleteForm: await superValidate(zod(deleteSchema)),
};
};
Expand All @@ -52,10 +53,12 @@ const createSchema = z
role: z.string().min(1).optional(),
startDatetime: z.date().optional(),
endDatetime: z.date().optional(),
information: z.string().optional(),
})
.refine((data) => data.studentId != null || data.role != null, {
message: "Du måste ange roll och studentid",
message: "Du måste ange roll eller student id",
});

const deleteSchema = z.object({
id: z.string(),
});
Expand Down Expand Up @@ -86,6 +89,32 @@ export const actions: Actions = {
type: "success",
});
},
ban: async ({ request, locals, params }) => {
const { prisma } = locals;
const form = await superValidate(request, zod(createSchema));
if (!form.valid) return fail(400, { form });
const doorName = params.slug;
const { studentId } = form.data;
if (
studentId &&
(await prisma.member.count({
where: { studentId },
})) <= 0
) {
return setError(form, "studentId", "Medlemmen finns inte");
}
await prisma.doorAccessPolicy.create({
data: {
doorName,
isBan: true,
...form.data,
},
});
return message(form, {
message: "Dörrpolicy skapad",
type: "success",
});
},
delete: async ({ request, locals }) => {
const { prisma } = locals;
const form = await superValidate(request, zod(deleteSchema));
Expand Down
Loading

0 comments on commit 36658d2

Please sign in to comment.