Skip to content

Commit 7907888

Browse files
committed
refactor: enhance API for setting random passwords with improved user handling
- Updated the `setRandomPasswordAndSendByEmail` API route to support `isNewUser` via both query string and request body. - Added logging for debugging purposes to track `isNewUser` status and email template selection. - Improved handling of user IDs input to accommodate different request formats.
1 parent 5ab8375 commit 7907888

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/imports/auth/password/email.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { templatePath } from '../../utils/templatesPath';
1515
import { logger } from '../../utils/logger';
1616

1717
export async function setRandomPasswordAndSendByEmail({ authTokenId, userIds, host, isNewUser = false }) {
18+
logger.debug(`[setRandomPasswordAndSendByEmail] isNewUser: ${isNewUser}, host: ${host}`);
1819
if (Array.isArray(userIds) === false) {
1920
return {
2021
success: false,
@@ -90,6 +91,8 @@ export async function setRandomPasswordAndSendByEmail({ authTokenId, userIds, ho
9091
// Seleciona o template apropriado baseado no contexto
9192
const templateName = isNewUser ? 'email/newUserPassword.html' : 'email/resetPassword.html';
9293
const emailSubject = isNewUser ? '[Auxiliadora Predial] Bem-vindo! Sua senha de acesso' : '[Konecty] Sua nova senha';
94+
95+
logger.debug(`[setRandomPasswordAndSendByEmail] Template: ${templateName}, Subject: ${emailSubject}, isNewUser: ${isNewUser}`);
9396

9497
const passwordTemplatePath = path.join(templatePath(), templateName);
9598

src/server/routes/rest/auth/authApi.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { StatusCodes } from 'http-status-codes';
66

77
import get from 'lodash/get';
88
import has from 'lodash/has';
9+
import isObject from 'lodash/isObject';
910
import isString from 'lodash/isString';
1011

1112
import { saveGeoLocation } from '@imports/auth/geolocation';
@@ -199,12 +200,27 @@ export const authApi: FastifyPluginCallback = (fastify, _, done) => {
199200
});
200201

201202
/* Set a random password for User and send by email */
202-
fastify.post<{ Body: string[]; Querystring: { isNewUser?: string } }>('/rest/auth/setRandomPasswordAndSendByEmail', async function (req, reply) {
203-
const authTokenId = getAuthTokenIdFromReq(req);
204-
const isNewUser = req.query.isNewUser === 'true';
205-
const result = await setRandomPasswordAndSendByEmail({ authTokenId, userIds: req.body, host: req.headers['host'], isNewUser });
206-
return reply.send(result);
207-
});
203+
fastify.post<{ Body: string[] | { userIds: string[]; isNewUser?: boolean }; Querystring: { isNewUser?: string } }>(
204+
'/rest/auth/setRandomPasswordAndSendByEmail',
205+
async function (req, reply) {
206+
const authTokenId = getAuthTokenIdFromReq(req);
207+
208+
// Suporta isNewUser via query string OU via body
209+
let isNewUser = req.query.isNewUser === 'true';
210+
let userIds = req.body;
211+
212+
// Se body é um objeto com userIds e isNewUser, processa
213+
if (isObject(req.body) && !Array.isArray(req.body) && req.body.userIds) {
214+
userIds = req.body.userIds;
215+
if (req.body.isNewUser === true) {
216+
isNewUser = true;
217+
}
218+
}
219+
220+
const result = await setRandomPasswordAndSendByEmail({ authTokenId, userIds, host: req.headers['host'], isNewUser });
221+
return reply.send(result);
222+
},
223+
);
208224

209225
done();
210226
};

0 commit comments

Comments
 (0)