@@ -3,6 +3,7 @@ import { generateClient } from "aws-amplify/data";
33import type { AppSyncIdentityCognito } from "aws-lambda" ;
44
55import type { Schema } from "../../../data/resource" ;
6+ import { tryCatch } from "../utils/try-catch" ;
67import { createTeam , updateUser } from "./graphql/mutations" ;
78import { 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+ } ) ;
4175export 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