Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified /writeUserSettings params #9124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ const updateUserEmailSettings = async ({
emailNotificationInterval,
promotionalEmailsEnabled,
}: UseUpdateUserEmailSettingsProps) => {
// TODO: cleanup https://github.com/hicommonwealth/commonwealth/issues/8393
const key = emailNotificationInterval
? 'updateEmailInterval'
: 'promotional_emails_enabled';
const value = emailNotificationInterval
? emailNotificationInterval
: `${promotionalEmailsEnabled}`;

await axios.post(
`${SERVER_URL}/${ApiEndpoints.UPDATE_USER_EMAIL_INTERVAL_SETTINGS}`,
{
jwt: userStore.getState().jwt,
key,
value,
...(typeof promotionalEmailsEnabled === 'boolean' && {
promotional_emails_enabled: promotionalEmailsEnabled,
}),
...(typeof emailNotificationInterval === 'string' && {
email_notification_interval: emailNotificationInterval,
}),
},
);

Expand Down
48 changes: 24 additions & 24 deletions packages/commonwealth/server/routes/writeUserSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const VALID_DIGEST_INTERVALS = [

export const Errors = {
InvalidUser: 'Invalid user',
NoKeyValue: 'Must provide key and value',
InvalidSetting: 'Invalid setting',
};

Expand All @@ -22,38 +21,39 @@ const writeUserSetting = async (
res: Response,
next: NextFunction,
) => {
const { key, value } = req.body;
const {
disable_rich_text,
promotional_emails_enabled,
email_notification_interval,
} = req.body;

if (!req.user) {
return next(new AppError(Errors.InvalidUser));
}
if (!key || !value) {
return next(new AppError(Errors.NoKeyValue));
}

if (key === 'disableRichText' && value === 'true') {
req.user.disableRichText = true;
await req.user.save();
} else if (key === 'disableRichText' && value === 'false') {
req.user.disableRichText = false;
await req.user.save();
} else if (
key === 'updateEmailInterval' &&
VALID_DIGEST_INTERVALS.indexOf(value) !== -1
) {
req.user.emailNotificationInterval = value;
await req.user.save();
} else if (
key === 'promotional_emails_enabled' &&
['true', 'false'].includes(value)
if (
!(
typeof disable_rich_text !== 'boolean' ||
typeof promotional_emails_enabled !== 'boolean' ||
email_notification_interval
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic seems incorrect. If disable_rich_text !== 'boolean' is true then the conditional evaluates to false and an InvalidSetting error is not returned.

)
) {
req.user.promotional_emails_enabled = value === 'true' ? true : false;
await req.user.save();
} else {
return next(new AppError(Errors.InvalidSetting));
}

return res.json({ status: 'Success', result: { key, value } });
if (typeof disable_rich_text === 'boolean') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff should be validated ahead of time and an error thrown earlier if the type is incorrect. If the conditionals are set up correct then Typescript should be able to automatically narrow down the type to Boolean.

req.user.disableRichText = disable_rich_text;
}
if (typeof promotional_emails_enabled === 'boolean') {
req.user.promotional_emails_enabled = promotional_emails_enabled;
}
if (VALID_DIGEST_INTERVALS.includes(email_notification_interval)) {
req.user.emailNotificationInterval = email_notification_interval;
}

await req.user.save();

return res.json({ status: 'Success', result: { user: req.user } });
};

export default writeUserSetting;
Loading