@@ -12,9 +12,13 @@ const {
1212 guardianPrivacyAuthTokenKey,
1313 acceptedKey,
1414 acceptedByKey,
15- latestTouchKey
15+ latestTouchKey,
16+ ageSetKey
1617} = require ( "./privacyKeys" ) ;
1718const debugRouter = require ( "./debugRouter" ) ;
19+ const {
20+ getGuardianEmails
21+ } = require ( "../getTypeSafeUser" ) ;
1822
1923router . use ( "/student/ultra-secret" , debugRouter ) ;
2024
@@ -140,14 +144,18 @@ router.get("/student/delay", studentMiddleware, async (req, res) => {
140144
141145router . post ( "/student/update" , studentMiddleware , async ( req , res ) => {
142146 try {
147+ const emails = mergeAccountEmailsWithReqBodyGuardianEmails ( req , res ) ;
148+ const hasGuardianEmails = emails . some ( e => e ?. type === 'guardian' ) ;
149+ const guardianTouchKey = req . user [ userNeedsGuardianTouchKey ] || Date . now ( ) ;
150+
143151 const updateBody = {
144- emails : mergeAccountEmailsWithReqBodyGuardianEmails ( req , res ) ,
152+ emails,
145153 guardianEmail : null ,
146154 [ guardianPrivacyAuthTokenKey ] :
147155 req . user [ guardianPrivacyAuthTokenKey ] ||
148156 generateGuardianPrivacyAuthToken ( ) ,
149157 [ userNeedsGuardianTouchKey ] :
150- req . user [ userNeedsGuardianTouchKey ] || Date . now ( ) ,
158+ hasGuardianEmails ? guardianTouchKey : null ,
151159 [ firstSeenKey ] : req . user [ firstSeenKey ] || Date . now ( ) ,
152160 [ dueByKey ] : req . user [ dueByKey ] || Date . now ( ) + SevenDays ,
153161 [ latestTouchKey ] : Date . now ( ) ,
@@ -168,6 +176,47 @@ router.post("/student/update", studentMiddleware, async (req, res) => {
168176 }
169177} ) ;
170178
179+ router . post ( "/student/update-age" , studentMiddleware , async ( req , res ) => {
180+ try {
181+ const month = req . body . month ;
182+ const year = req . body . year ;
183+
184+ if (
185+ typeof month !== "number" ||
186+ month < 1 ||
187+ month > 12 ||
188+ typeof year !== "number" ||
189+ year < 1900 ||
190+ year > new Date ( ) . getFullYear ( )
191+ ) {
192+ return res . status ( 200 ) . send ( {
193+ success : false ,
194+ error : "Invalid month or year" ,
195+ data : null ,
196+ } ) ;
197+ }
198+
199+ const updateBody = {
200+ birthMonth : String ( month ) ,
201+ birthYear : String ( year ) ,
202+ [ ageSetKey ] : Date . now ( ) ,
203+ } ;
204+
205+ await userDB . doc ( req . user . uid ) . update ( updateBody ) ;
206+
207+ return res
208+ . status ( 200 )
209+ . send ( { success : true , data : updateBody , error : null } ) ;
210+ } catch ( e ) {
211+ console . error ( "Failed to update age" , req . user . uid , e ) ;
212+ return res . status ( 200 ) . send ( {
213+ success : false ,
214+ error : "Failed to update age" ,
215+ data : null ,
216+ } ) ;
217+ }
218+ } ) ;
219+
171220router . get ( "/student/accept" , studentMiddleware , async ( req , res ) => {
172221 try {
173222 const updateBody = {
0 commit comments