Skip to content

Commit

Permalink
refactor: make use of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
simonknittel committed Dec 19, 2024
1 parent e96c417 commit 58818ff
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { authenticateApi } from "@/auth/server";
import { prisma } from "@/db";
import { updateActiveMembership } from "@/organizations/utils/updateActiveMembership";
import { ConfirmationStatus } from "@prisma/client";
import { ConfirmationStatus, OrganizationMembershipType } from "@prisma/client";
import { NextResponse } from "next/server";
import { z } from "zod";
import apiErrorHandler from "../../../../../../../lib/apiErrorHandler";
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function DELETE(request: Request, { params }: { params: Params }) {
id: paramsData.citizenId,
},
},
type: "LEFT",
type: OrganizationMembershipType.LEFT,
visibility: membership.visibility,
createdBy: {
connect: {
Expand Down
3 changes: 2 additions & 1 deletion app/src/app/api/spynet/organization/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { authenticateApi } from "@/auth/server";
import { prisma } from "@/db";
import { log } from "@/logging";
import { ConfirmationStatus } from "@prisma/client";
import { NextResponse } from "next/server";
import { serializeError } from "serialize-error";
import { z } from "zod";
Expand Down Expand Up @@ -76,7 +77,7 @@ export async function POST(request: Request) {
},
attributeKey: "name",
newValue: data.name,
confirmed: "CONFIRMED",
confirmed: ConfirmationStatus.CONFIRMED,
confirmedAt: new Date(),
confirmedBy: {
connect: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { requireAuthentication } from "@/auth/server";
import {
OrganizationMembershipType,
OrganizationMembershipVisibility,
type Entity,
type Organization,
type OrganizationMembershipHistoryEntry,
Expand Down Expand Up @@ -35,13 +37,17 @@ export const mapOrganizationMembershipHistoryEntries = async (

return entries
.filter((entry) =>
(alsoVisibilityRedacted ? ["PUBLIC", "REDACTED"] : ["PUBLIC"]).includes(
entry.visibility,
),
(alsoVisibilityRedacted
? [
OrganizationMembershipVisibility.PUBLIC,
OrganizationMembershipVisibility.REDACTED,
]
: [OrganizationMembershipVisibility.PUBLIC]
).includes(entry.visibility),
)
.map((entry) => {
switch (entry.type) {
case "MAIN":
case OrganizationMembershipType.MAIN:
return {
key: entry.id,
date: entry.createdAt,
Expand Down Expand Up @@ -76,7 +82,7 @@ export const mapOrganizationMembershipHistoryEntries = async (
),
};

case "AFFILIATE":
case OrganizationMembershipType.AFFILIATE:
return {
key: entry.id,
date: entry.createdAt,
Expand All @@ -101,7 +107,7 @@ export const mapOrganizationMembershipHistoryEntries = async (
),
};

case "LEFT":
case OrganizationMembershipType.LEFT:
return {
key: entry.id,
date: entry.createdAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { useAuthentication } from "@/auth/client";
import {
type OrganizationMembershipType,
type OrganizationMembershipVisibility,
ConfirmationStatus,
OrganizationMembershipType,
OrganizationMembershipVisibility,
} from "@prisma/client";
import { useRouter } from "next/navigation";
import { useId, useState } from "react";
Expand Down Expand Up @@ -35,7 +36,7 @@ export const CreateOrganizationMembership = ({
const router = useRouter();
const { register, handleSubmit, reset } = useForm<FormValues>({
defaultValues: {
type: "MAIN",
type: OrganizationMembershipType.MAIN,
},
});
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -62,7 +63,7 @@ export const CreateOrganizationMembership = ({
redacted: data.visibility || false,
confirmed:
e.nativeEvent.submitter.name === "confirmed"
? "CONFIRMED"
? ConfirmationStatus.CONFIRMED
: undefined,
}),
},
Expand Down Expand Up @@ -108,7 +109,7 @@ export const CreateOrganizationMembership = ({

<form onSubmit={handleSubmit(onSubmit)}>
<label className="mt-6 block" htmlFor={organizationIdInputId}>
Organisation
Organisation (Sinister ID)
</label>

<input
Expand All @@ -127,8 +128,10 @@ export const CreateOrganizationMembership = ({
id={typeInputId}
{...register("type", { required: true })}
>
<option value="MAIN">Main</option>
<option value="AFFILIATE">Affiliate</option>
<option value={OrganizationMembershipType.MAIN}>Main</option>
<option value={OrganizationMembershipType.AFFILIATE}>
Affiliate
</option>
</select>

<div className="mt-6 flex justify-between items-center">
Expand All @@ -137,7 +140,7 @@ export const CreateOrganizationMembership = ({
<YesNoCheckbox
{...register("visibility")}
id={visibilityInputId}
value="REDACTED"
value={OrganizationMembershipVisibility.REDACTED}
/>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { requireAuthentication } from "@/auth/server";
import { prisma } from "@/db";
import { OrganizationMembershipVisibility } from "@prisma/client";
import clsx from "clsx";
import Image from "next/image";
import Link from "next/link";
Expand Down Expand Up @@ -33,17 +34,16 @@ export const OrganizationMembershipsTile = async ({ className, id }: Props) => {
where: {
citizenId: id,
visibility: {
in: alsoVisibilityRedacted ? ["PUBLIC", "REDACTED"] : ["PUBLIC"],
in: alsoVisibilityRedacted
? [
OrganizationMembershipVisibility.PUBLIC,
OrganizationMembershipVisibility.REDACTED,
]
: [OrganizationMembershipVisibility.PUBLIC],
},
},
select: {
organization: {
select: {
id: true,
name: true,
logo: true,
},
},
include: {
organization: true,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { requireAuthentication } from "@/auth/server";
import { prisma } from "@/db";
import {
ConfirmationStatus,
OrganizationMembershipType,
OrganizationMembershipVisibility,
} from "@prisma/client";
import clsx from "clsx";
Expand Down Expand Up @@ -116,7 +117,7 @@ export const ActivityTile = async ({ className, id }: Props) => {
}),
...organization.membershipHistoryEntries.map((entry) => {
switch (entry.type) {
case "MAIN":
case OrganizationMembershipType.MAIN:
return {
key: entry.id,
date: entry.createdAt,
Expand All @@ -135,7 +136,7 @@ export const ActivityTile = async ({ className, id }: Props) => {
),
};

case "AFFILIATE":
case OrganizationMembershipType.AFFILIATE:
return {
key: entry.id,
date: entry.createdAt,
Expand All @@ -154,7 +155,7 @@ export const ActivityTile = async ({ className, id }: Props) => {
),
};

case "LEFT":
case OrganizationMembershipType.LEFT:
return {
key: entry.id,
date: entry.createdAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { useAuthentication } from "@/auth/client";
import {
type OrganizationMembershipType,
type OrganizationMembershipVisibility,
ConfirmationStatus,
OrganizationMembershipType,
OrganizationMembershipVisibility,
} from "@prisma/client";
import { useRouter } from "next/navigation";
import { useId, useState } from "react";
Expand Down Expand Up @@ -32,7 +33,7 @@ export const CreateMembership = ({ className, organizationId }: Props) => {
const router = useRouter();
const { register, handleSubmit, reset } = useForm<FormValues>({
defaultValues: {
type: "MAIN",
type: OrganizationMembershipType.MAIN,
},
});
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -59,7 +60,7 @@ export const CreateMembership = ({ className, organizationId }: Props) => {
redacted: data.visibility || false,
confirmed:
e.nativeEvent.submitter.name === "confirmed"
? "CONFIRMED"
? ConfirmationStatus.CONFIRMED
: undefined,
}),
},
Expand Down Expand Up @@ -124,8 +125,10 @@ export const CreateMembership = ({ className, organizationId }: Props) => {
id={typeInputId}
{...register("type", { required: true })}
>
<option value="MAIN">Main</option>
<option value="AFFILIATE">Affiliate</option>
<option value={OrganizationMembershipType.MAIN}>Main</option>
<option value={OrganizationMembershipType.AFFILIATE}>
Affiliate
</option>
</select>

<div className="mt-6 flex justify-between items-center">
Expand All @@ -134,7 +137,7 @@ export const CreateMembership = ({ className, organizationId }: Props) => {
<YesNoCheckbox
{...register("visibility")}
id={visibilityInputId}
value="REDACTED"
value={OrganizationMembershipVisibility.REDACTED}
/>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { requireAuthentication } from "@/auth/server";
import { prisma } from "@/db";
import { OrganizationMembershipVisibility } from "@prisma/client";
import clsx from "clsx";
import Link from "next/link";
import { FaExternalLinkAlt, FaUsers } from "react-icons/fa";
Expand Down Expand Up @@ -32,7 +33,12 @@ export const MembershipsTile = async ({ className, id }: Props) => {
where: {
organizationId: id,
visibility: {
in: alsoVisibilityRedacted ? ["PUBLIC", "REDACTED"] : ["PUBLIC"],
in: alsoVisibilityRedacted
? [
OrganizationMembershipVisibility.PUBLIC,
OrganizationMembershipVisibility.REDACTED,
]
: [OrganizationMembershipVisibility.PUBLIC],
},
},
select: {
Expand Down

0 comments on commit 58818ff

Please sign in to comment.