@@ -12,9 +12,13 @@ const {
12
12
guardianPrivacyAuthTokenKey,
13
13
acceptedKey,
14
14
acceptedByKey,
15
- latestTouchKey
15
+ latestTouchKey,
16
+ ageSetKey
16
17
} = require ( "./privacyKeys" ) ;
17
18
const debugRouter = require ( "./debugRouter" ) ;
19
+ const {
20
+ getGuardianEmails
21
+ } = require ( "../getTypeSafeUser" ) ;
18
22
19
23
router . use ( "/student/ultra-secret" , debugRouter ) ;
20
24
@@ -140,14 +144,18 @@ router.get("/student/delay", studentMiddleware, async (req, res) => {
140
144
141
145
router . post ( "/student/update" , studentMiddleware , async ( req , res ) => {
142
146
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
+
143
151
const updateBody = {
144
- emails : mergeAccountEmailsWithReqBodyGuardianEmails ( req , res ) ,
152
+ emails,
145
153
guardianEmail : null ,
146
154
[ guardianPrivacyAuthTokenKey ] :
147
155
req . user [ guardianPrivacyAuthTokenKey ] ||
148
156
generateGuardianPrivacyAuthToken ( ) ,
149
157
[ userNeedsGuardianTouchKey ] :
150
- req . user [ userNeedsGuardianTouchKey ] || Date . now ( ) ,
158
+ hasGuardianEmails ? guardianTouchKey : null ,
151
159
[ firstSeenKey ] : req . user [ firstSeenKey ] || Date . now ( ) ,
152
160
[ dueByKey ] : req . user [ dueByKey ] || Date . now ( ) + SevenDays ,
153
161
[ latestTouchKey ] : Date . now ( ) ,
@@ -168,6 +176,47 @@ router.post("/student/update", studentMiddleware, async (req, res) => {
168
176
}
169
177
} ) ;
170
178
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
+
171
220
router . get ( "/student/accept" , studentMiddleware , async ( req , res ) => {
172
221
try {
173
222
const updateBody = {
0 commit comments