Skip to content

Commit

Permalink
fix: SJIP-725 types
Browse files Browse the repository at this point in the history
  • Loading branch information
aperron-ferlab committed Mar 19, 2024
1 parent 1b2be07 commit 7ce2950
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
20 changes: 10 additions & 10 deletions src/db/dal/newsletter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { keycloakRealm } from '../../config/env';
import { getNewsletterStatusFetcher, getNewsletterUpdater, SubscriptionStatus } from '../../utils/newsletter';
import UserModel, { IUserOuput } from '../models/User';
import UserModel, { IUserOutput } from '../models/User';
import { getUserById } from './user';

export const subscribeNewsletter = async (keycloak_id: string, email: string): Promise<IUserOuput> => {
export const subscribeNewsletter = async (keycloak_id: string, email: string): Promise<IUserOutput> => {
const newsletterUpdater = getNewsletterUpdater(keycloakRealm);

const user = await getUserById(keycloak_id, true);

if (newsletterUpdater) {
const newsletterStatus = await newsletterUpdater({
user: user,
user,
action: SubscriptionStatus.SUBSCRIBED,
email,
});
Expand All @@ -24,7 +24,7 @@ export const subscribeNewsletter = async (keycloak_id: string, email: string): P
return user;
};

export const unsubscribeNewsletter = async (keycloak_id: string): Promise<IUserOuput> => {
export const unsubscribeNewsletter = async (keycloak_id: string): Promise<IUserOutput> => {
const newsletterUpdater = getNewsletterUpdater(keycloakRealm);

const user = await getUserById(keycloak_id, true);
Expand All @@ -33,7 +33,7 @@ export const unsubscribeNewsletter = async (keycloak_id: string): Promise<IUserO
const newsletterStatus = await newsletterUpdater({
user: user as UserModel,
action: SubscriptionStatus.UNSUBSCRIBED,
email: user.dataValues.newsletter_email,
email: user.newsletter_email,
});

return updateUserNewsletterInfo(keycloak_id, {
Expand All @@ -45,16 +45,16 @@ export const unsubscribeNewsletter = async (keycloak_id: string): Promise<IUserO
return user;
};

export const refreshNewsletterStatus = async (keycloak_id: string): Promise<IUserOuput> => {
export const refreshNewsletterStatus = async (keycloak_id: string): Promise<IUserOutput> => {
const newsletterStatusFetcher = getNewsletterStatusFetcher(keycloakRealm);

const user = await getUserById(keycloak_id, true);
const isSubscribed = user.dataValues.newsletter_subscription_status === SubscriptionStatus.SUBSCRIBED;
const isSubscribed = user.newsletter_subscription_status === SubscriptionStatus.SUBSCRIBED;

if (newsletterStatusFetcher && isSubscribed) {
const newsletterStatus = await newsletterStatusFetcher(user.dataValues.newsletter_email);
const newsletterStatus = await newsletterStatusFetcher(user.newsletter_email);

if (newsletterStatus !== user.dataValues.newsletter_subscription_status) {
if (newsletterStatus !== user.newsletter_subscription_status) {
return updateUserNewsletterInfo(keycloak_id, {
newsletter_subscription_status: newsletterStatus,
newsletter_email: newsletterStatus === SubscriptionStatus.UNSUBSCRIBED ? null : undefined,
Expand All @@ -71,7 +71,7 @@ export const updateUserNewsletterInfo = async (
newsletter_subscription_status: SubscriptionStatus;
newsletter_email?: string | null;
},
): Promise<IUserOuput> => {
): Promise<IUserOutput> => {
const { newsletter_subscription_status, newsletter_email } = payload;
const updatedUser = await UserModel.update(
{
Expand Down
19 changes: 9 additions & 10 deletions src/db/dal/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { uuid } from 'uuidv4';
import { profileImageBucket } from '../../config/env';
import config from '../../config/project';
import { UserValidator } from '../../utils/userValidator';
import UserModel, { IUserInput } from '../models/User';
import UserModel, { IUserInput, IUserOutput } from '../models/User';

let S3Client;
try {
Expand All @@ -17,7 +17,6 @@ try {
}

const sanitizeInputPayload = (payload: IUserInput) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {
id,
keycloak_id,
Expand Down Expand Up @@ -149,7 +148,7 @@ export const getProfileImageUploadPresignedUrl = async (keycloak_id: string) =>
};
};

export const getUserById = async (keycloak_id: string, isOwn: boolean): Promise<UserModel> => {
export const getUserById = async (keycloak_id: string, isOwn: boolean): Promise<IUserOutput> => {
let attributesClause = {};
if (!isOwn) {
attributesClause = {
Expand All @@ -169,7 +168,7 @@ export const getUserById = async (keycloak_id: string, isOwn: boolean): Promise<
throw createHttpError(StatusCodes.NOT_FOUND, `User with keycloak id ${keycloak_id} does not exist.`);
}

return user;
return user.dataValues;
};

export const isUserExists = async (
Expand All @@ -188,7 +187,7 @@ export const isUserExists = async (
};
};

export const createUser = async (keycloak_id: string, payload: IUserInput): Promise<UserModel> => {
export const createUser = async (keycloak_id: string, payload: IUserInput): Promise<IUserOutput> => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { newsletter_email, newsletter_subscription_status, ...rest } = payload;

Expand All @@ -199,10 +198,10 @@ export const createUser = async (keycloak_id: string, payload: IUserInput): Prom
updated_date: new Date(),
});

return newUser;
return newUser.dataValues;
};

export const updateUser = async (keycloak_id: string, payload: IUserInput): Promise<UserModel> => {
export const updateUser = async (keycloak_id: string, payload: IUserInput): Promise<IUserOutput> => {
const results = await UserModel.update(
{
...sanitizeInputPayload(payload),
Expand All @@ -216,7 +215,7 @@ export const updateUser = async (keycloak_id: string, payload: IUserInput): Prom
},
);

return results[1][0];
return results[1][0].dataValues;
};

export const deleteUser = async (keycloak_id: string): Promise<void> => {
Expand Down Expand Up @@ -249,7 +248,7 @@ export const completeRegistration = async (
keycloak_id: string,
payload: IUserInput,
validator: UserValidator,
): Promise<UserModel> => {
): Promise<IUserOutput> => {
if (!validator(payload)) {
throw createHttpError(
StatusCodes.BAD_REQUEST,
Expand All @@ -271,7 +270,7 @@ export const completeRegistration = async (
},
);

return results[1][0];
return results[1][0].dataValues;
};

export const resetAllConsents = async (): Promise<number> => {
Expand Down
3 changes: 2 additions & 1 deletion src/db/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ interface IUserAttributes {

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IUserInput extends IUserAttributes {}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IUserOuput extends IUserAttributes {}
export interface IUserOutput extends IUserAttributes {}

class UserModel extends Model<IUserAttributes, IUserInput> implements IUserAttributes {
public id: number;
Expand Down
2 changes: 1 addition & 1 deletion src/external/smartsheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const handleNewsletterUpdate = async (payload: NewsletterPayload): Promis

if (!rowId && payload.action === SubscriptionStatus.SUBSCRIBED) {
const formattedRow = formatRow(smartsheet.columns, {
...payload.user.dataValues,
...payload.user,
newsletter_email: payload.email,
});

Expand Down
4 changes: 2 additions & 2 deletions src/external/smartsheetTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IUserOuput } from '../db/models/User';
import { IUserOutput } from '../db/models/User';

// The type could be better but this is most likely sufficient for our use case
// In case we need to update the types, it's Greatly inspired from:
Expand Down Expand Up @@ -69,7 +69,7 @@ export type Row = {
};

export type SubscribeNewsletterPayload = Pick<
IUserOuput,
IUserOutput,
'first_name' | 'last_name' | 'affiliation' | 'roles' | 'newsletter_email'
>;

Expand Down
2 changes: 2 additions & 0 deletions src/routes/newsletter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ newsletterRouter.put('/subscribe', async (req, res, next) => {
try {
const keycloak_id = req['kauth']?.grant?.access_token?.content?.sub;
const result = await subscribeNewsletter(keycloak_id, req.body.newsletter_email);

console.log(result);
res.status(StatusCodes.OK).send(result);
} catch (e) {
next(e);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/newsletter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Realm from '../config/realm';
import UserModel from '../db/models/User';
import { IUserOutput } from '../db/models/User';
import { getSubscriptionStatus, handleNewsletterUpdate } from '../external/smartsheet';

export enum SubscriptionStatus {
Expand All @@ -9,7 +9,7 @@ export enum SubscriptionStatus {
}

export type NewsletterPayload = {
user: UserModel;
user: IUserOutput;
email: string;
action: SubscriptionStatus;
};
Expand Down

0 comments on commit 7ce2950

Please sign in to comment.