Skip to content

Commit 06bc31e

Browse files
committed
remove checkin and add devPost attribute
1 parent c94f43e commit 06bc31e

File tree

16 files changed

+252
-181
lines changed

16 files changed

+252
-181
lines changed

amplify/auth/PostConfirmation/handler.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export const handler: PostConfirmationTriggerHandler = async (event) => {
5757
role: "Participant",
5858
id: event.request.userAttributes.sub,
5959
email: event.request.userAttributes.email,
60-
checkedIn: false,
6160
willEatMeals: false,
6261
allergies: "",
6362
institution: "",

amplify/data/resource.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ const schema = a
3434
completedRegistration: a.boolean(),
3535
allergies: a.string(),
3636
willEatMeals: a.boolean(),
37-
checkedIn: a
38-
.boolean()
39-
.default(false)
40-
.authorization((allow) => [
41-
allow.ownerDefinedIn("profileOwner").to(["read"]),
42-
allow.groups(["Admin"]).to(["read", "update", "delete", "create"]),
43-
]),
4437
teamId: a
4538
.id()
4639
.authorization((allow) => [
@@ -94,6 +87,12 @@ const schema = a
9487
members: a.hasMany("User", "teamId"),
9588
scores: a.hasMany("Score", "teamId"),
9689
teamRooms: a.hasMany("TeamRoom", "teamId"),
90+
devPostLink: a
91+
.string()
92+
.authorization((allow) => [
93+
allow.groups(["Admin"]).to(["read", "update"]),
94+
allow.authenticated().to(["read", "update"]),
95+
]),
9796
})
9897
.authorization((allow) => [
9998
allow.group("Admin").to(["read", "update", "create", "delete"]),
@@ -279,21 +278,6 @@ const schema = a
279278
.authorization((allow) => [allow.group("Admin")])
280279
.handler(a.handler.function(ResetHackathon))
281280
.returns(a.ref("StatusCodeFunctionResponse")),
282-
283-
// Custom resolvers
284-
SetUserAsCheckedIn: a
285-
.mutation()
286-
.arguments({
287-
userId: a.string().required(),
288-
})
289-
.returns(a.ref("User"))
290-
.authorization((allow) => [allow.authenticated()])
291-
.handler(
292-
a.handler.custom({
293-
dataSource: a.ref("User"),
294-
entry: "./user/SetUserAsCheckedIn.js",
295-
}),
296-
),
297281
})
298282

299283
.authorization((allow) => [

amplify/data/user/SetUserAsCheckedIn.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

amplify/function/BusinessLogic/CreateTeamWithCode/handler.ts

Lines changed: 90 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Amplify } from "aws-amplify";
22
import { generateClient } from "aws-amplify/data";
33
import type { AppSyncIdentityCognito } from "aws-lambda";
44
import type { Schema } from "../../../data/resource";
5+
import { tryCatch } from "../utils/try-catch";
56
import { createTeam, updateUser } from "./graphql/mutations";
67
import { getTeam } from "./graphql/queries";
78

@@ -37,101 +38,106 @@ const client = generateClient<Schema>({
3738
authMode: "iam",
3839
});
3940

41+
const createNewTeam = (teamName: string, teamId: string) =>
42+
client.graphql({
43+
query: createTeam,
44+
variables: {
45+
input: {
46+
name: teamName,
47+
id: teamId,
48+
},
49+
},
50+
});
51+
const updateUserTeam = (id: string, teamId: string) =>
52+
client.graphql({
53+
query: updateUser,
54+
variables: {
55+
input: {
56+
id,
57+
teamId,
58+
},
59+
},
60+
});
61+
const generateTeamId = () =>
62+
Array.from(Array(4), () =>
63+
Math.floor(Math.random() * 36)
64+
.toString(36)
65+
.toUpperCase(),
66+
).join("");
67+
const getTeamFromId = (teamId: string) =>
68+
client.graphql({
69+
query: getTeam,
70+
variables: {
71+
id: teamId,
72+
},
73+
});
4074
export const handler: Schema["CreateTeamWithCode"]["functionHandler"] = async (
4175
event,
4276
) => {
43-
let team = null;
44-
let teamId: string | null = null;
45-
try {
46-
do {
47-
teamId = Array.from(Array(4), () =>
48-
Math.floor(Math.random() * 36)
49-
.toString(36)
50-
.toUpperCase(),
51-
).join("");
77+
const {
78+
arguments: { addCallerToTeam, teamName },
79+
} = event;
5280

53-
team = (
54-
await client.graphql({
55-
query: getTeam,
56-
variables: {
57-
id: teamId,
58-
},
59-
})
60-
).data.getTeam;
61-
} while (team != null);
81+
let teamId = generateTeamId();
82+
let { error: teamIdTaken } = await tryCatch(getTeamFromId(teamId));
83+
let attempts = 0;
84+
const MAX_ATTEMPTS = 100; // Define a maximum number of attempts
6285

63-
const teamCreation = await client
64-
.graphql({
65-
query: createTeam,
66-
variables: {
67-
input: {
68-
name: event.arguments.teamName,
69-
id: teamId,
70-
},
71-
},
72-
})
73-
.catch(() => {
74-
throw new Error(
75-
JSON.stringify({
76-
body: { value: `Error creating team` },
77-
statusCode: 500,
78-
headers: { "Content-Type": "application/json" },
79-
}),
80-
);
81-
});
86+
while (teamIdTaken && attempts < MAX_ATTEMPTS) {
87+
teamId = generateTeamId();
88+
({ error: teamIdTaken } = await tryCatch(getTeamFromId(teamId)));
89+
attempts++;
90+
}
8291

83-
if (teamCreation) {
84-
if (event.arguments.addCallerToTeam) {
85-
return await client
86-
.graphql({
87-
query: updateUser,
88-
variables: {
89-
input: {
90-
id: (event.identity as AppSyncIdentityCognito).sub,
91-
teamId: teamId,
92-
},
93-
},
94-
})
95-
.then(() => {
96-
return {
97-
body: { value: teamId },
98-
statusCode: 200,
99-
headers: { "Content-Type": "application/json" },
100-
};
101-
})
102-
.catch(() => {
103-
throw new Error(
104-
JSON.stringify({
105-
body: { value: `Error updating user (team was created)` },
106-
statusCode: 500,
107-
headers: { "Content-Type": "application/json" },
108-
}),
109-
);
110-
});
111-
} else {
112-
return {
113-
body: { value: teamId },
114-
statusCode: 200,
115-
headers: { "Content-Type": "application/json" },
116-
};
117-
}
118-
} else {
119-
throw new Error(
120-
JSON.stringify({
121-
body: { value: `Error creating team` },
122-
statusCode: 500,
123-
headers: { "Content-Type": "application/json" },
124-
}),
125-
);
126-
}
127-
} catch (error) {
128-
console.error(error);
92+
if (teamIdTaken) {
93+
// Handle the case where a unique team ID could not be generated
94+
throw new Error(
95+
JSON.stringify({
96+
body: {
97+
value: `Failed to generate a unique team ID after ${MAX_ATTEMPTS} attempts.`,
98+
},
99+
statusCode: 500,
100+
headers: { "Content-Type": "application/json" },
101+
}),
102+
);
103+
}
104+
const { error: createTeamError } = await tryCatch(
105+
createNewTeam(teamName, teamId),
106+
);
107+
if (createTeamError) {
129108
throw new Error(
130109
JSON.stringify({
131-
body: { value: `Unhandled Internal Server Error` },
110+
body: { value: `Error creating team` },
132111
statusCode: 500,
133112
headers: { "Content-Type": "application/json" },
134113
}),
135114
);
136115
}
116+
if (!addCallerToTeam) {
117+
return {
118+
body: { value: teamId },
119+
statusCode: 200,
120+
headers: { "Content-Type": "application/json" },
121+
};
122+
}
123+
const { error: updateUserError, data: updateUserSuccess } = await tryCatch(
124+
updateUserTeam((event.identity as AppSyncIdentityCognito).sub, teamId),
125+
);
126+
if (updateUserSuccess) {
127+
return {
128+
body: { value: teamId },
129+
statusCode: 200,
130+
headers: { "Content-Type": "application/json" },
131+
};
132+
}
133+
134+
throw new Error(
135+
JSON.stringify({
136+
body: {
137+
value: `Error updating user ( ${(event.identity as AppSyncIdentityCognito).sub}) (team was created) ${updateUserError}`,
138+
},
139+
statusCode: 500,
140+
headers: { "Content-Type": "application/json" },
141+
}),
142+
);
137143
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Types for the result object with discriminated union
2+
type Success<T> = {
3+
data: T;
4+
error: null;
5+
};
6+
7+
type Failure<E> = {
8+
data: null;
9+
error: E;
10+
};
11+
12+
type Result<T, E = Error> = Success<T> | Failure<E>;
13+
14+
// Main wrapper function
15+
export async function tryCatch<T, E = Error>(
16+
promise: Promise<T>,
17+
): Promise<Result<T, E>> {
18+
try {
19+
const data = await promise;
20+
return { data, error: null };
21+
} catch (error) {
22+
return { data: null, error: error as E };
23+
}
24+
}

src/app/admin/teams/TeamTableSetup.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ export const teamColumns = [
4747
filterFn: "includesString",
4848
sortingFn: "alphanumeric",
4949
}),
50-
columnHelper.accessor("members", {
51-
cell: (info) =>
52-
info.getValue().every((member) => member.checkedIn)
53-
? "Checked In"
54-
: "Not Checked In",
55-
header: "Check-in Status",
56-
sortingFn: "basic",
57-
}),
5850
columnHelper.accessor("approved", {
5951
cell: ({
6052
getValue,
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
import type { Schema } from "@/amplify/data/resource";
21
import client from "@/components/_Amplify/AmplifyBackendClient";
32
import TeamsTable from "./TeamsTable";
43

5-
type Members = Pick<
6-
Schema["User"]["type"],
7-
"id" | "firstName" | "lastName" | "checkedIn"
8-
>;
9-
export type Team = Pick<Schema["Team"]["type"], "name" | "approved" | "id"> & {
10-
members: Members[];
11-
};
4+
const getTeams = client.models.Team.list({
5+
selectionSet: [
6+
"name",
7+
"approved",
8+
"id",
9+
"devPostLink",
10+
"members.id",
11+
"members.firstName",
12+
"members.lastName",
13+
],
14+
});
15+
export type Team = Awaited<typeof getTeams>["data"][number];
1216
export default async function TeamsTablePage() {
13-
const { data: teams } = await client.models.Team.list({
14-
selectionSet: [
15-
"name",
16-
"approved",
17-
"id",
18-
"members.id",
19-
"members.firstName",
20-
"members.lastName",
21-
"members.checkedIn",
22-
],
23-
});
17+
const { data: teams } = await getTeams;
2418
if (!teams || !Array.isArray(teams)) return "No teams were found";
2519
return <TeamsTable teams={teams} />;
2620
}

src/app/admin/teams/components/ViewButton.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export default function ViewButton({ team }: { team: Team }) {
3131
<td className={"rounded-md p-2"}>
3232
{`${member.firstName} ${member.lastName}`}
3333
</td>
34-
<td className={"rounded-md p-2"}>
35-
{member.checkedIn ? "Checked In" : "Not Checked In"}
36-
</td>
34+
<td className={"rounded-md p-2"}>{member.id}</td>
3735
</tr>
3836
))}
3937
</tbody>

src/app/participant/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export default function page() {
2828
<div className="flex flex-col gap-4">
2929
<NextMealScheduled />
3030
<GoToFoodTicket />
31-
{/* <DevPostSubmission /> */}
3231
</div>
3332
<div className="flex flex-col gap-4">
3433
<ImportantInformation />

0 commit comments

Comments
 (0)