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

chore: remove waitlist UI, emails, and admin APIs #123

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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

This file was deleted.

This file was deleted.

This file was deleted.

69 changes: 0 additions & 69 deletions api/src/api/admin.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
use crate::analysis::RegistryLoader;
use crate::buckets::Buckets;
use crate::emails::EmailArgs;
use crate::emails::EmailSender;
use crate::NpmUrl;
use crate::RegistryUrl;
use hyper::Body;
use hyper::Request;
use routerify::prelude::RequestExt;
use routerify::Router;
use std::borrow::Cow;
use std::sync::Arc;
use tracing::field;
use tracing::instrument;
Expand All @@ -35,12 +31,7 @@ pub fn admin_router() -> Router<Body, ApiError> {
.get("/aliases", util::auth(util::json(list_aliases)))
.post("/aliases", util::auth(util::json(create_alias)))
.get("/users", util::auth(util::json(list_users)))
.get("/users/waitlisted", util::auth(util::json(list_waitlisted)))
.patch("/users/:user_id", util::auth(util::json(update_user)))
.post(
"/users/:user_id/waitlist_accept",
util::auth(util::json(waitlist_accept_user)),
)
.get("/scopes", util::auth(util::json(list_scopes)))
.patch("/scopes/:scope", util::auth(util::json(patch_scopes)))
.get(
Expand Down Expand Up @@ -110,26 +101,6 @@ pub async fn list_users(req: Request<Body>) -> ApiResult<ApiList<ApiFullUser>> {
})
}

#[instrument(name = "GET /api/admin/users/waitlisted", skip(req), err)]
pub async fn list_waitlisted(
req: Request<Body>,
) -> ApiResult<ApiList<ApiFullUser>> {
let iam = req.iam();
iam.check_admin_access()?;

let db = req.data::<Database>().unwrap();
let (start, limit) = pagination(&req);
let maybe_search = search(&req);

let (total_users, users) =
db.list_users_waitlisted(start, limit, maybe_search).await?;

Ok(ApiList {
items: users.into_iter().map(|user| user.into()).collect(),
total: total_users,
})
}

#[instrument(
name = "PATCH /api/admin/users/:user_id",
skip(req),
Expand Down Expand Up @@ -170,46 +141,6 @@ pub async fn update_user(mut req: Request<Body>) -> ApiResult<ApiFullUser> {
}
}

#[instrument(
name = "PATCH /api/admin/users/:user_id/waitlist_accept",
skip(req),
err,
fields(user_id)
)]
pub async fn waitlist_accept_user(
req: Request<Body>,
) -> ApiResult<ApiFullUser> {
let iam = req.iam();
iam.check_admin_access()?;

let user_id = req.param_uuid("user_id")?;
Span::current().record("user_id", &field::display(&user_id));
let db = req.data::<Database>().unwrap();
let email_sender = req.data::<Option<EmailSender>>().unwrap();
let registry_url = req.data::<RegistryUrl>().unwrap();

let user = db.get_user(user_id).await?.ok_or(ApiError::UserNotFound)?;
if user.waitlist_accepted_at.is_some() {
return Ok(user.into());
}

let updated_user = db.user_waitlist_accept(user_id).await?;

if let Some(email_sender) = email_sender {
if let Some(email_addr) = user.email {
let email = EmailArgs::WaitlistAccept {
name: Cow::Borrowed(&updated_user.name),
registry_url: Cow::Borrowed(registry_url.0.as_str()),
registry_name: Cow::Borrowed(&email_sender.from_name),
support_email: Cow::Borrowed(&email_sender.from),
};
email_sender.send(email_addr, email).await?;
}
}

Ok(updated_user.into())
}

#[instrument(name = "GET /api/admin/scopes", skip(req), err)]
pub async fn list_scopes(
req: Request<Body>,
Expand Down
48 changes: 0 additions & 48 deletions api/src/db/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,39 +133,6 @@ impl Database {
Ok((total_users as usize, users))
}

#[instrument(name = "Database::list_users_waitlisted", skip(self), err)]
pub async fn list_users_waitlisted(
&self,
start: i64,
limit: i64,
maybe_search_query: Option<&str>,
) -> Result<(usize, Vec<User>)> {
let mut tx = self.pool.begin().await?;
let search = format!("%{}%", maybe_search_query.unwrap_or(""));
let users = sqlx::query_as!(
User,
r#"SELECT id, name, email, avatar_url, updated_at, created_at, github_id, is_blocked, is_staff, scope_limit, waitlist_accepted_at,
(SELECT COUNT(created_at) FROM scope_invites WHERE target_user_id = id) as "invite_count!",
(SELECT COUNT(created_at) FROM scopes WHERE creator = id) as "scope_usage!"
FROM users WHERE waitlist_accepted_at IS NULL AND (name ILIKE $1 OR email ILIKE $1) ORDER BY created_at ASC OFFSET $2 LIMIT $3"#,
search,
start,
limit,
)
.fetch_all(&mut *tx)
.await?;

let total_waitlisted = sqlx::query!(
r#"SELECT COUNT(created_at) as "count!" FROM users WHERE waitlist_accepted_at IS NULL AND (name ILIKE $1 OR email ILIKE $1);"#,
search,
)
.map(|r| r.count)
.fetch_one(&mut *tx)
.await?;

Ok((total_waitlisted as usize, users))
}

#[instrument(name = "Database::insert_user", skip(self, new_user), err, fields(user.name = new_user.name, user.email = new_user.email, user.avatar_url = new_user.avatar_url, user.github_id = new_user.github_id, user.is_blocked = new_user.is_blocked, user.is_staff = new_user.is_staff))]
pub async fn insert_user(&self, new_user: NewUser<'_>) -> Result<User> {
sqlx::query_as!(
Expand Down Expand Up @@ -247,21 +214,6 @@ impl Database {
.await
}

#[instrument(name = "Database::user_waitlist_accept", skip(self), err)]
pub async fn user_waitlist_accept(&self, user_id: Uuid) -> Result<User> {
sqlx::query_as!(
User,
r#"UPDATE users SET waitlist_accepted_at = now() WHERE id = $1
RETURNING id, name, email, avatar_url, updated_at, created_at, github_id, is_blocked, is_staff, scope_limit, waitlist_accepted_at,
(SELECT COUNT(created_at) FROM scope_invites WHERE target_user_id = id) as "invite_count!",
(SELECT COUNT(created_at) FROM scopes WHERE creator = id) as "scope_usage!"
"#,
user_id
)
.fetch_one(&self.pool)
.await
}

#[instrument(name = "Database::delete_user", skip(self), err)]
pub async fn delete_user(&self, id: Uuid) -> Result<Option<User>> {
sqlx::query_as!(
Expand Down
Loading
Loading