Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 7d74d4e

Browse files
✨ Whether email is primary email of user
1 parent fe97488 commit 7d74d4e

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

src/crud/email.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import {
22
query,
33
tableValues,
44
setValues,
5-
removeReadOnlyValues
5+
removeReadOnlyValues,
6+
addIsPrimaryToEmails,
7+
addIsPrimaryToEmail
68
} from "../helpers/mysql";
79
import { Email } from "../interfaces/tables/emails";
810
import { dateToDateTime } from "../helpers/utils";
@@ -20,7 +22,11 @@ import { deleteItemFromCache, cachedQuery } from "../helpers/cache";
2022
* @param sendVerification Whether to send an email verification link to new email
2123
* @param isVerified Whether this email is verified by default
2224
*/
23-
export const createEmail = async (email: Email, sendVerification = true, isVerified = false) => {
25+
export const createEmail = async (
26+
email: Email,
27+
sendVerification = true,
28+
isVerified = false
29+
) => {
2430
email.email = email.email.toLowerCase();
2531
email.isVerified = isVerified;
2632
email.createdAt = new Date();
@@ -122,7 +128,9 @@ export const getUserPrimaryEmailObject = async (user: User | number) => {
122128
}
123129
const primaryEmailId = userObject.primaryEmail;
124130
if (!primaryEmailId) throw new Error(ErrorCode.MISSING_PRIMARY_EMAIL);
125-
return await getEmail(primaryEmailId);
131+
const email = await getEmail(primaryEmailId);
132+
email.isPrimary = true;
133+
return email;
126134
};
127135

128136
/**
@@ -136,28 +144,30 @@ export const getUserPrimaryEmail = async (user: User | number) => {
136144
* Get a list of all emails added by a user
137145
*/
138146
export const getUserEmails = async (userId: number) => {
139-
return <Email>(
147+
return await addIsPrimaryToEmails(<Email[]>(
140148
await cachedQuery(
141149
CacheCategories.USER_EMAILS,
142150
userId,
143151
"SELECT * FROM emails WHERE userId = ?",
144152
[userId]
145153
)
146-
);
154+
));
147155
};
148156

149157
/**
150158
* Get the detailed email object from an email
151159
*/
152160
export const getEmailObject = async (email: string) => {
153-
return (<Email[]>(
154-
await cachedQuery(
155-
CacheCategories.EMAIL,
156-
email,
157-
"SELECT * FROM emails WHERE email = ? LIMIT 1",
158-
[email]
159-
)
160-
))[0];
161+
return await addIsPrimaryToEmail(
162+
(<Email[]>(
163+
await cachedQuery(
164+
CacheCategories.EMAIL,
165+
email,
166+
"SELECT * FROM emails WHERE email = ? LIMIT 1",
167+
[email]
168+
)
169+
))[0]
170+
);
161171
};
162172

163173
/**
@@ -171,12 +181,12 @@ export const getUserVerifiedEmails = async (user: User | number) => {
171181
userId = user;
172182
}
173183
if (!userId) throw new Error(ErrorCode.USER_NOT_FOUND);
174-
return <Email[]>(
184+
return await addIsPrimaryToEmails(<Email[]>(
175185
await cachedQuery(
176186
CacheCategories.USER_VERIFIED_EMAILS,
177187
userId,
178188
"SELECT * FROM emails WHERE userId = ? AND isVerified = 1",
179189
[userId]
180190
)
181-
);
191+
));
182192
};

src/helpers/mysql.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Organization } from "../interfaces/tables/organization";
1414
import { Event } from "../interfaces/tables/events";
1515
import { KeyValue } from "../interfaces/general";
1616
import { boolValues, jsonValues, dateValues, readOnlyValues } from "./utils";
17+
import { getUserPrimaryEmailObject } from "../crud/email";
1718

1819
export const pool = createPool({
1920
host: DB_HOST,
@@ -121,3 +122,20 @@ export const removeReadOnlyValues = (object: KeyValue) => {
121122
});
122123
return object;
123124
};
125+
126+
export const addIsPrimaryToEmails = async (emails: Email[]) => {
127+
const userPrimaryEmailObject = await getUserPrimaryEmailObject(
128+
emails[0].userId
129+
);
130+
emails.map(email => {
131+
email.isPrimary = email.id === userPrimaryEmailObject.id;
132+
return email;
133+
});
134+
return emails;
135+
};
136+
137+
export const addIsPrimaryToEmail = async (email: Email) => {
138+
const userPrimaryEmailObject = await getUserPrimaryEmailObject(email.userId);
139+
email.isPrimary = email.id === userPrimaryEmailObject.id;
140+
return email;
141+
};

src/interfaces/tables/emails.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface Email {
33
email: string;
44
userId: number;
55
isVerified?: boolean;
6+
isPrimary?: boolean;
67
createdAt?: Date;
78
updatedAt?: Date;
89
}

0 commit comments

Comments
 (0)