1- /* eslint-disable no-unused-vars */
2- /* eslint-disable no-underscore-dangle */
3- /* eslint-disable no-shadow */
4- /* eslint-disable consistent-return */
1+ /* eslint-disable */
2+
53import express , { Router } from "express"
64import axios from "axios"
75import session from "express-session"
@@ -76,6 +74,9 @@ router.post("/authenticate", async (req, res, next) => {
7674 req . session . regenerate ( ( err ) => {
7775 if ( err ) return next ( err )
7876 req . session . memberId = member . _id
77+ req . session . discordId = data . id
78+ req . session . username = data . username
79+ req . session . discordAvatar = data . avatar
7980 req . session . save ( ( err ) => {
8081 if ( err ) return next ( err )
8182 console . log ( "Authenticated successfully" )
@@ -102,21 +103,26 @@ router.get("/me", async (req, res, next) => {
102103 // maybe refactor this lol
103104 const { associationMembership } = member
104105
105- /*
106106 res . json ( {
107107 username : member . username ,
108108 _id : member . _id ,
109+ discord : {
110+ avatar : req . session . discordAvatar ,
111+ id : req . session . discordId
112+ } ,
109113 associationMembership : {
110114 firstName : associationMembership . firstName ,
111115 lastName : associationMembership . lastName ,
112116 city : associationMembership . city ,
113117 googleWorkspaceName : associationMembership . googleWorkspaceName ,
114118 email : associationMembership . email ,
119+ acceptedAt : associationMembership . acceptedAt ,
115120 handledIn : associationMembership . handledIn ,
116121 status : associationMembership . status
117122 }
118123 } )
119- */
124+
125+ /*
120126 res.json({
121127 username: member.username,
122128 _id: member._id,
@@ -129,16 +135,84 @@ router.get("/me", async (req, res, next) => {
129135 handledIn: associationMembership.handledIn,
130136 status: associationMembership.status
131137 }
132- } )
138+ }) */
133139 } catch ( e ) {
134140 next ( e )
135141 }
136142} )
137143
144+ router . post ( "/apply" , async ( req , res ) => {
145+ try {
146+ const id = req . session . discordId ;
147+ const username = req . session . username ;
148+
149+ console . log ( "New assoc application " , username , req . body . email )
150+
151+ // extract the rest data from request
152+ let { firstName, lastName, city, email } = req . body ;
153+ const fieldsMissing = firstName . trim ( ) . length == 0 ||
154+ lastName . trim ( ) . length == 0 ||
155+ city . trim ( ) . length == 0 ||
156+ email . trim ( ) . length == 0 ||
157+ ! / ^ \S + @ \S + $ / . test ( email ) ;
158+ if ( fieldsMissing ) throw "fields missing"
159+
160+ // check if email address already belongs to an assoc member
161+ // if so, swap it to a random one (so that we don't give out info whether someone is a member or not)
162+ const resultEmail = await database . UserInfo . findOne ( {
163+ "associationMembership.email" : email ,
164+ "id" : { $ne : id }
165+ } )
166+ if ( resultEmail ) {
167+ email = new Date ( ) . getTime ( ) + '@testausapis-duplikaatti-email'
168+ }
169+
170+ // check if Discord member is already a member of the association
171+ const resultDiscord = await database . UserInfo . getUserInfo ( id )
172+ if ( resultDiscord ?. associationMembership . status == 'MEMBER' ) throw "dc already assoc member"
173+
174+ // upsert application
175+ const appliedAt = new Date ( ) ;
176+ const doc = await database . UserInfo . findOneAndUpdate ( { id } , {
177+ associationMembership : {
178+ firstName,
179+ lastName,
180+ city,
181+ email,
182+ appliedAt,
183+ status : "RECEIVED"
184+ }
185+ } , { upsert : true , new : true } )
186+ console . log ( doc ) ;
187+
188+ // give http response
189+ res . json ( { status : "ok" } )
190+
191+ // invoke webhook
192+ const webhookData = {
193+ firstName,
194+ lastName,
195+ city,
196+ email,
197+ username,
198+ appliedAt
199+ }
200+ await axios . post ( process . env . APPLY_WEBHOOK , webhookData )
201+ } catch ( e ) {
202+ console . log ( e )
203+ res . status ( 500 ) . json ( { status : "error" } )
204+ }
205+ } )
206+
138207router . get ( "/logout" , async ( req , res , next ) => {
139208 try {
140209 req . session . destroy ( )
141210 console . log ( "User logged out" )
211+ if ( req . query . state === "opener" ) {
212+ res . setHeader ( "Content-Type" , "text/html" )
213+ res . end ( "<script>parent.opener.postMessage('logout');window.close();</script>" )
214+ return
215+ }
142216 res . redirect ( "/" )
143217 } catch ( e ) {
144218 next ( e )
0 commit comments