Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

fix: fix getUser endpoint in dev #2327

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/email.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const {
SIB_APIKEY_PRIVATE,
} = EMAIL_CONFIG;

if (process.env.NODE_ENV !== 'test') {
if (process.env.NODE_ENV === 'prod') {
try {
const sendInBlue = makeSendinblue({
MAIL_SENDER,
Expand Down
51 changes: 35 additions & 16 deletions src/controllers/communityController.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as Sentry from '@sentry/node';

import config from '../config';
import BetaGouv from '../betagouv';
import * as utils from './utils';
Expand All @@ -8,6 +10,7 @@ import betagouv from '../betagouv';
import { EMAIL_STATUS_READABLE_FORMAT } from '@/models/misc';
import { MattermostUser, getUserByEmail, searchUsers } from '@/lib/mattermost';
import { getContactInfo } from '@/config/email.config';
import { DBUser } from '@/models/dbUser';

export async function getCommunity(req, res) {
getCommunityPageData(
Expand Down Expand Up @@ -148,6 +151,29 @@ export async function getUser(req, res) {
);
}

const getMattermostUserInfo = async (dbUser) => {
let mattermostUser: MattermostUser, mattermostUserInTeamAndActive: boolean;
try {
mattermostUser = dbUser?.primary_email
? await getUserByEmail(dbUser.primary_email).catch((e) => null)
: null;
[mattermostUserInTeamAndActive] = dbUser?.primary_email
? await searchUsers({
term: dbUser.primary_email,
team_id: config.mattermostTeamId,
allow_inactive: false,
}).catch((e) => [])
: [];
} catch (e) {
Sentry.captureException(e);
}

return {
mattermostUser,
mattermostUserInTeamAndActive,
};
};

async function getUserPageData(req, res, onSuccess, onError) {
const { username } = req.params;
const isCurrentUser = req.auth.id === username;
Expand All @@ -171,31 +197,24 @@ async function getUserPageData(req, res, onSuccess, onError) {
.where({ username });
const marrainageState = marrainageStateResponse[0];

const dbUser = await knex('users').where({ username }).first();
const dbUser: DBUser | undefined = await knex('users')
.where({ username })
.first();
const secondaryEmail = dbUser ? dbUser.secondary_email : '';
let availableEmailPros = [];
if (config.ESPACE_MEMBRE_ADMIN.includes(req.auth.id)) {
availableEmailPros = await betagouv.getAvailableProEmailInfos();
}
let mattermostUser: MattermostUser = dbUser?.primary_email
? await getUserByEmail(dbUser.primary_email).catch((e) => null)
: null;
let [mattermostUserInTeamAndActive]: MattermostUser[] =
dbUser?.primary_email
? await searchUsers({
term: dbUser.primary_email,
team_id: config.mattermostTeamId,
allow_inactive: false,
}).catch((e) => [])
: [];
let { mattermostUser, mattermostUserInTeamAndActive } =
await getMattermostUserInfo(dbUser);
let emailServiceInfo = {};
if (dbUser.primary_email) {
emailServiceInfo['primary_email'] = await getContactInfo({
if (dbUser?.primary_email) {
emailServiceInfo['primaryEmail'] = await getContactInfo({
email: dbUser.primary_email,
});
}
if (dbUser.secondary_email) {
emailServiceInfo['secondary_email'] = await getContactInfo({
if (dbUser?.secondary_email) {
emailServiceInfo['secondaryEmail'] = await getContactInfo({
email: dbUser.secondary_email,
});
}
Expand Down
5 changes: 2 additions & 3 deletions tests/test-community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ describe('Community', () => {
});

it('should have information about user email in email service', async () => {
const res = await chai.request(app).get('/api/community/membre.expire');
console.log(res.body.emailServiceInfo);
res.body.emailServiceInfo['primary_email'].should.be.a('object');
const res = await chai.request(app).get('/api/community/membre.actif');
res.body.emailServiceInfo['primaryEmail'].should.be.a('object');
});
});
});
Loading