Skip to content

Commit 1dea38f

Browse files
committed
claimed
1 parent 51ed899 commit 1dea38f

File tree

8 files changed

+56
-23
lines changed

8 files changed

+56
-23
lines changed

lib/api_organizations/src/organizations.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ async fn get_ls_inner(
100100
))?;
101101

102102
// Drop connection lock before iterating
103-
let json_organizations = organizations
103+
let json_organizations = conn_lock!(context, |conn| organizations
104104
.into_iter()
105-
.map(QueryOrganization::into_json)
106-
.collect();
105+
.map(|org| org.into_json(conn))
106+
.collect());
107107

108108
let total_count = get_ls_query(context, auth_user, &pagination_params, &query_params)
109109
.count()
@@ -184,7 +184,7 @@ async fn post_inner(
184184
InsertOrganization::from_json(conn_lock!(context), json_organization)?;
185185
let query_organization =
186186
QueryOrganization::create(context, auth_user, insert_organization).await?;
187-
Ok(query_organization.into_json())
187+
Ok(query_organization.into_json(conn_lock!(context)))
188188
}
189189

190190
#[derive(Deserialize, JsonSchema)]
@@ -230,14 +230,16 @@ async fn get_one_inner(
230230
path_params: OrganizationParams,
231231
auth_user: &AuthUser,
232232
) -> Result<JsonOrganization, HttpError> {
233-
Ok(QueryOrganization::is_allowed_resource_id(
234-
conn_lock!(context),
235-
&context.rbac,
236-
&path_params.organization,
237-
auth_user,
238-
Permission::View,
239-
)?
240-
.into_json())
233+
conn_lock!(context, |conn| {
234+
Ok(QueryOrganization::is_allowed_resource_id(
235+
conn,
236+
&context.rbac,
237+
&path_params.organization,
238+
auth_user,
239+
Permission::View,
240+
)?
241+
.into_json(conn))
242+
})
241243
}
242244

243245
/// Update an organization
@@ -319,7 +321,11 @@ async fn patch_inner(
319321
.execute(conn_lock!(context))
320322
.map_err(resource_conflict_err!(Organization, update_organization))?;
321323

322-
Ok(QueryOrganization::get(conn_lock!(context), query_organization.id)?.into_json())
324+
conn_lock!(context, |conn| Ok(QueryOrganization::get(
325+
conn,
326+
query_organization.id
327+
)?
328+
.into_json(conn)))
323329
}
324330

325331
/// Delete an organization

lib/api_organizations/src/projects.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ async fn get_ls_inner(
128128
))?;
129129

130130
// Drop connection lock before iterating
131-
let json_projects = projects
131+
let json_projects = conn_lock!(context, |conn| projects
132132
.into_iter()
133-
.map(|project| project.into_json_for_organization(&query_organization))
134-
.collect();
133+
.map(|project| project.into_json_for_organization(conn, &query_organization))
134+
.collect());
135135

136136
let total_count = get_ls_query(&query_organization, &pagination_params, &query_params)
137137
.count()

lib/bencher_json/src/organization/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct JsonOrganization {
4545
pub license: Option<bencher_valid::Jwt>,
4646
pub created: DateTime,
4747
pub modified: DateTime,
48+
pub claimed: Option<DateTime>,
4849
}
4950

5051
// Unfortunately, we have to use a complex, custom type and deserializer here.

lib/bencher_json/src/project/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub struct JsonProject {
6767
pub visibility: Visibility,
6868
pub created: DateTime,
6969
pub modified: DateTime,
70+
pub claimed: Option<DateTime>,
7071
}
7172

7273
impl Display for JsonProject {

lib/bencher_schema/src/model/organization/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ impl QueryOrganization {
230230
Ok(total_members > 0)
231231
}
232232

233-
pub fn into_json(self) -> JsonOrganization {
233+
pub fn claimed_at(&self, conn: &mut DbConnection) -> Result<DateTime, HttpError> {
234+
QueryOrganizationRole::claimed_at(conn, self.id)
235+
}
236+
237+
pub fn into_json(self, conn: &mut DbConnection) -> JsonOrganization {
238+
let claimed = self.claimed_at(conn).ok();
234239
let Self {
235240
uuid,
236241
name,
@@ -249,6 +254,7 @@ impl QueryOrganization {
249254
license,
250255
created,
251256
modified,
257+
claimed,
252258
}
253259
}
254260
}

lib/bencher_schema/src/model/organization/organization_role.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ impl QueryOrganizationRole {
3838
.get_result(conn)
3939
.map_err(resource_not_found_err!(OrganizationRole, organization_id))
4040
}
41+
42+
pub fn claimed_at(
43+
conn: &mut DbConnection,
44+
organization_id: OrganizationId,
45+
) -> Result<DateTime, HttpError> {
46+
schema::organization_role::table
47+
.filter(schema::organization_role::organization_id.eq(&organization_id))
48+
.select(schema::organization_role::created)
49+
.order(schema::organization_role::created.asc())
50+
.first(conn)
51+
.map_err(resource_not_found_err!(OrganizationRole, organization_id))
52+
}
4153
}
4254

4355
#[derive(Debug, diesel::Insertable)]

lib/bencher_schema/src/model/project/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,16 @@ impl QueryProject {
410410
}
411411

412412
pub fn into_json(self, conn: &mut DbConnection) -> Result<JsonProject, HttpError> {
413-
let query_organization = QueryOrganization::get(conn, self.organization_id)?;
414-
Ok(self.into_json_for_organization(&query_organization))
413+
let query_organization: QueryOrganization =
414+
QueryOrganization::get(conn, self.organization_id)?;
415+
Ok(self.into_json_for_organization(conn, &query_organization))
415416
}
416417

417-
pub fn into_json_for_organization(self, organization: &QueryOrganization) -> JsonProject {
418+
pub fn into_json_for_organization(
419+
self,
420+
conn: &mut DbConnection,
421+
query_organization: &QueryOrganization,
422+
) -> JsonProject {
418423
let Self {
419424
uuid,
420425
organization_id,
@@ -428,19 +433,21 @@ impl QueryProject {
428433
} = self;
429434
assert_parentage(
430435
BencherResource::Organization,
431-
organization.id,
436+
query_organization.id,
432437
BencherResource::Project,
433438
organization_id,
434439
);
440+
let claimed = query_organization.claimed_at(conn).ok();
435441
JsonProject {
436442
uuid,
437-
organization: organization.uuid,
443+
organization: query_organization.uuid,
438444
name,
439445
slug,
440446
url,
441447
visibility,
442448
created,
443449
modified,
450+
claimed,
444451
}
445452
}
446453
}

lib/bencher_schema/src/model/server/stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn get_stats(
4242
.load::<QueryOrganization>(conn)
4343
.map_err(resource_not_found_err!(Organization))?
4444
.into_iter()
45-
.map(QueryOrganization::into_json)
45+
.map(|org| org.into_json(conn))
4646
.collect(),
4747
)
4848
};

0 commit comments

Comments
 (0)