Skip to content

Commit e4e3cd3

Browse files
committed
remove checkin and add devPost attribute
1 parent 394db77 commit e4e3cd3

File tree

18 files changed

+302
-426
lines changed

18 files changed

+302
-426
lines changed

amplify/auth/PostConfirmation/amplify_outputs.json

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,6 @@
113113
"isRequired": false,
114114
"attributes": []
115115
},
116-
"checkedIn": {
117-
"name": "checkedIn",
118-
"isArray": false,
119-
"type": "Boolean",
120-
"isRequired": false,
121-
"attributes": []
122-
},
123116
"teamId": {
124117
"name": "teamId",
125118
"isArray": false,
@@ -1263,22 +1256,6 @@
12631256
"isRequired": true
12641257
}
12651258
}
1266-
},
1267-
"SetUserAsCheckedIn": {
1268-
"name": "SetUserAsCheckedIn",
1269-
"isArray": false,
1270-
"type": {
1271-
"model": "User"
1272-
},
1273-
"isRequired": false,
1274-
"arguments": {
1275-
"userId": {
1276-
"name": "userId",
1277-
"isArray": false,
1278-
"type": "String",
1279-
"isRequired": true
1280-
}
1281-
}
12821259
}
12831260
}
12841261
}

amplify/auth/PostConfirmation/handler.ts

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

amplify/data/resource.ts

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

300284
.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: 75 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { generateClient } from "aws-amplify/data";
33
import type { AppSyncIdentityCognito } from "aws-lambda";
44

55
import type { Schema } from "../../../data/resource";
6+
import { tryCatch } from "../utils/try-catch";
67
import { createTeam, updateUser } from "./graphql/mutations";
78
import { getTeam } from "./graphql/queries";
89

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

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

64-
const teamCreation = await client
65-
.graphql({
66-
query: createTeam,
67-
variables: {
68-
input: {
69-
name: event.arguments.teamName,
70-
id: teamId,
71-
},
72-
},
73-
})
74-
.catch(() => {
75-
throw new Error(
76-
JSON.stringify({
77-
body: { value: `Error creating team` },
78-
statusCode: 500,
79-
headers: { "Content-Type": "application/json" },
80-
}),
81-
);
82-
});
83-
84-
if (teamCreation) {
85-
if (event.arguments.addCallerToTeam) {
86-
return await client
87-
.graphql({
88-
query: updateUser,
89-
variables: {
90-
input: {
91-
id: (event.identity as AppSyncIdentityCognito).sub,
92-
teamId: teamId,
93-
},
94-
},
95-
})
96-
.then(() => {
97-
return {
98-
body: { value: teamId },
99-
statusCode: 200,
100-
headers: { "Content-Type": "application/json" },
101-
};
102-
})
103-
.catch(() => {
104-
throw new Error(
105-
JSON.stringify({
106-
body: { value: `Error updating user (team was created)` },
107-
statusCode: 500,
108-
headers: { "Content-Type": "application/json" },
109-
}),
110-
);
111-
});
112-
} else {
113-
return {
114-
body: { value: teamId },
115-
statusCode: 200,
116-
headers: { "Content-Type": "application/json" },
117-
};
118-
}
119-
} else {
120-
throw new Error(
121-
JSON.stringify({
122-
body: { value: `Error creating team` },
123-
statusCode: 500,
124-
headers: { "Content-Type": "application/json" },
125-
}),
126-
);
127-
}
128-
} catch (error) {
129-
console.error(error);
82+
let teamId = generateTeamId();
83+
let { error: teamIdTaken } = await tryCatch(getTeamFromId(teamId));
84+
while (teamIdTaken) {
85+
teamId = generateTeamId();
86+
({ error: teamIdTaken } = await tryCatch(getTeamFromId(teamId)));
87+
} // possibility of infite loop at 36^4 teams
88+
const { error: createTeamError } = await tryCatch(
89+
createNewTeam(teamName, teamId),
90+
);
91+
if (createTeamError) {
13092
throw new Error(
13193
JSON.stringify({
132-
body: { value: `Unhandled Internal Server Error` },
94+
body: { value: `Error creating team` },
13395
statusCode: 500,
13496
headers: { "Content-Type": "application/json" },
13597
}),
13698
);
13799
}
100+
if (!addCallerToTeam) {
101+
return {
102+
body: { value: teamId },
103+
statusCode: 200,
104+
headers: { "Content-Type": "application/json" },
105+
};
106+
}
107+
const { error: updateUserError, data: updateUserSuccess } = await tryCatch(
108+
updateUserTeam((event.identity as AppSyncIdentityCognito).sub, teamId),
109+
);
110+
if (updateUserSuccess) {
111+
return {
112+
body: { value: teamId },
113+
statusCode: 200,
114+
headers: { "Content-Type": "application/json" },
115+
};
116+
}
117+
118+
throw new Error(
119+
JSON.stringify({
120+
body: {
121+
value: `Error updating user ( ${(event.identity as AppSyncIdentityCognito).sub}) (team was created) ${updateUserError}`,
122+
},
123+
statusCode: 500,
124+
headers: { "Content-Type": "application/json" },
125+
}),
126+
);
138127
};

0 commit comments

Comments
 (0)