22 query ,
33 tableValues ,
44 setValues ,
5- removeReadOnlyValues
5+ removeReadOnlyValues ,
6+ addIsPrimaryToEmails ,
7+ addIsPrimaryToEmail
68} from "../helpers/mysql" ;
79import { Email } from "../interfaces/tables/emails" ;
810import { 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 */
138146export 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 */
152160export 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} ;
0 commit comments