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

Commit

Permalink
fix: fix getUser endpoint in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCharrier committed Jan 11, 2024
1 parent 78a4ad5 commit 2ed65e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
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';

Check failure on line 1 in src/controllers/communityController.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

'MattermostUser' is declared but its value is never read.

Check failure on line 1 in src/controllers/communityController.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'MattermostUser' is declared but its value is never read.

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, mattermostUserInTeamAndActive;
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

0 comments on commit 2ed65e9

Please sign in to comment.