@@ -75,6 +75,26 @@ export class DBClient {
7575 }
7676 }
7777
78+ /**
79+ * Gets a user by user id
80+ *
81+ * @param {number } id
82+ * @returns
83+ */
84+ async getUserById ( id ) {
85+ const { data, error, status } = await this . client
86+ . from ( 'user' )
87+ . select ( '*' )
88+ . eq ( 'id' , id )
89+ . single ( )
90+
91+ if ( error ) {
92+ throw new DBError ( error )
93+ }
94+
95+ return data
96+ }
97+
7898 /**
7999 * Get user by magic.link or old github id
80100 *
@@ -130,6 +150,75 @@ export class DBClient {
130150 return data ?. status === 'Blocked'
131151 }
132152
153+ /**
154+ * Gets user tag change requests
155+ *
156+ * @param {number } userId
157+ */
158+ async getUserRequests ( userId ) {
159+ const { data, error } = await this . client
160+ . from ( 'user_tag_proposal' )
161+ . select ( '*' )
162+ . match ( { user_id : userId } )
163+ . is ( 'deleted_at' , null )
164+
165+ if ( error ) {
166+ throw new DBError ( error )
167+ }
168+
169+ return data
170+ }
171+
172+ /**
173+ * Creates a user tag change request
174+ *
175+ * @param {number } userId
176+ * @param {string } tagName
177+ * @param {string } requestedTagValue
178+ * @param {JSON } userProposalForm
179+ * @returns
180+ */
181+ async createUserRequest (
182+ userId ,
183+ tagName ,
184+ requestedTagValue ,
185+ userProposalForm
186+ ) {
187+ const { data : deleteData , status : deleteStatus } = await this . client
188+ . from ( 'user_tag_proposal' )
189+ . update ( {
190+ deleted_at : new Date ( ) . toISOString ( ) ,
191+ } )
192+ . match ( { user_id : userId , tag : tagName } )
193+ . is ( 'deleted_at' , null )
194+
195+ if (
196+ deleteStatus === 200 ||
197+ ( ( deleteStatus === 406 || deleteStatus === 404 ) && ! deleteData )
198+ ) {
199+ const { error : insertError , status : insertStatus } = await this . client
200+ . from ( 'user_tag_proposal' )
201+ . insert ( {
202+ user_id : userId ,
203+ tag : tagName ,
204+ proposed_tag_value : requestedTagValue ,
205+ inserted_at : new Date ( ) . toISOString ( ) ,
206+ user_proposal_form : userProposalForm ,
207+ } )
208+ . single ( )
209+
210+ if ( insertError ) {
211+ throw new DBError ( insertError )
212+ }
213+
214+ if ( insertStatus === 201 ) {
215+ return true
216+ }
217+ }
218+
219+ return false
220+ }
221+
133222 /**
134223 * Create upload with content and pins
135224 *
0 commit comments