import { NextApiRequest, NextApiResponse } from 'next'; import { UpdateNotificationsParams } from '../../src/model/components/profile'; import { SetNotificationInfoParams, SetNotificationParams, Team, User, } from '../../src/model/user'; import { ApiMethods, ApiRoutes } from '../../src/model/api'; import { CustomResponse } from '../../src/model/common'; import { RegisterParams } from '../../src/model/components/signUp'; import authenticateRequest from '../../src/utils/api/authenticateRequest'; const register = async ( req: NextApiRequest, res: NextApiResponse>, ): Promise => { const { name, licenseNumber, email, department, password, team, avatar, isExistingTeam, } = req.body; if ( !name || !licenseNumber || !email || !department || !password || !team || !avatar ) { return res.status(200).json({ ok: false, error: 'MISSING_PARAMS' }); } const registerParams: RegisterParams = { email, password, username: name, licenseNumber, avatar, department, team, }; const response = await authenticateRequest<{ jwt: string }, RegisterParams>( ApiMethods.Post, ApiRoutes.Register, registerParams, ); const { ok, data, error } = response; if (!ok) { return res.status(200).json({ ok, error }); } // #region notifications for team users if (isExistingTeam) { const newUserResponse = await authenticateRequest( ApiMethods.Get, `${ApiRoutes.GetUser}?email=${email}`, ); const newUserId = newUserResponse.data[0]._id; const teamResponse = await authenticateRequest( ApiMethods.Get, `${ApiRoutes.GetTeams}/${team}`, ); const teamUsers = teamResponse.data?.users; const currentDate: string = new Date().toISOString().split('T')[0]; const newUserNotification: SetNotificationInfoParams = { text: 'NEW_USER_NOTIFICATION', newUser: newUserId, requestedTeam: teamResponse.data.id, }; const updateUsersPromise: Promise | string>[] = []; teamUsers.forEach((teamUser) => { // temporal fix (in future newUser wont be in team) if (teamUser._id === newUserId) { updateUsersPromise.push(Promise.resolve('Oye que soy yo mismo')); return; } const updatedNotifications: SetNotificationParams[] = teamUser.notifications; const notificationIndex = teamUser.notifications.findIndex( (notification) => notification.date === currentDate, ); if (notificationIndex !== -1) { const updatedNotification: SetNotificationInfoParams[] = teamUser.notifications[notificationIndex].newNotificationsInfo || []; updatedNotification.push(newUserNotification); updatedNotifications[notificationIndex].newNotificationsInfo = updatedNotification; } else { updatedNotifications.push({ date: currentDate, newNotificationsInfo: [newUserNotification], notificationsInfo: [], }); } updateUsersPromise.push( Promise.resolve( authenticateRequest( ApiMethods.Put, `${ApiRoutes.UpdateUser}/${teamUser._id}`, { notifications: updatedNotifications, }, ), ), ); }); await Promise.all(updateUsersPromise); } // #endregion notifications for team users const { jwt } = data; res.setHeader('Set-Cookie', `dia_token=${jwt}; Path=/;`); return res.status(200).json({ ok: true, data: jwt }); }; export default register;