Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web-backend): add organization S3 bucket accessors

Richie McIlroy745c75d91273
diff --git a/packages/web-backend/src/S3Buckets/S3BucketsRepo.ts b/packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
index 1b06f9a46..6fd618105 100644
--- a/packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
+++ b/packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
@@ -1,5 +1,10 @@
import * as Db from "@cap/database/schema";
-import { S3Bucket, type User, type Video } from "@cap/web-domain";
+import {
+ type Organisation,
+ S3Bucket,
+ type User,
+ type Video,
+} from "@cap/web-domain";
import * as Dz from "drizzle-orm";
import { Effect, Option } from "effect";
@@ -55,7 +60,14 @@ export class S3BucketsRepo extends Effect.Service<S3BucketsRepo>()(
db
.select({ bucket: Db.s3Buckets })
.from(Db.s3Buckets)
- .where(Dz.eq(Db.s3Buckets.ownerId, userId)),
+ .where(
+ Dz.and(
+ Dz.eq(Db.s3Buckets.ownerId, userId),
+ Dz.isNull(Db.s3Buckets.organizationId),
+ Dz.eq(Db.s3Buckets.active, true),
+ ),
+ )
+ .orderBy(Dz.desc(Db.s3Buckets.updatedAt)),
);
return Option.fromNullable(res).pipe(
@@ -66,7 +78,31 @@ export class S3BucketsRepo extends Effect.Service<S3BucketsRepo>()(
}),
);
- return { getForVideo, getById, getForUser };
+ const getForOrganization = Effect.fn("S3BucketsRepo.getForOrganization")(
+ (organizationId: Organisation.OrganisationId) =>
+ Effect.gen(function* () {
+ const [res] = yield* db.use((db) =>
+ db
+ .select({ bucket: Db.s3Buckets })
+ .from(Db.s3Buckets)
+ .where(
+ Dz.and(
+ Dz.eq(Db.s3Buckets.organizationId, organizationId),
+ Dz.eq(Db.s3Buckets.active, true),
+ ),
+ )
+ .orderBy(Dz.desc(Db.s3Buckets.updatedAt)),
+ );
+
+ return Option.fromNullable(res).pipe(
+ Option.map((v) =>
+ S3Bucket.decodeSync({ ...v.bucket, name: v.bucket.bucketName }),
+ ),
+ );
+ }),
+ );
+
+ return { getForVideo, getById, getForUser, getForOrganization };
}),
dependencies: [Database.Default],
},
diff --git a/packages/web-backend/src/S3Buckets/index.ts b/packages/web-backend/src/S3Buckets/index.ts
index 3e1c6254b..64a6c481b 100644
--- a/packages/web-backend/src/S3Buckets/index.ts
+++ b/packages/web-backend/src/S3Buckets/index.ts
@@ -1,7 +1,7 @@
import * as S3 from "@aws-sdk/client-s3";
import * as CloudFrontPresigner from "@aws-sdk/cloudfront-signer";
import { decrypt } from "@cap/database/crypto";
-import type { S3Bucket, User } from "@cap/web-domain";
+import type { Organisation, S3Bucket, User } from "@cap/web-domain";
import type { RequestPresigningArguments } from "@smithy/types";
import { Config, Effect, Layer, Option } from "effect";
@@ -200,6 +200,17 @@ export class S3Buckets extends Effect.Service<S3Buckets>()("S3Buckets", {
);
},
),
+ getBucketAccessForOrganization: Effect.fn(
+ "S3Buckets.getBucketAccessForOrganization",
+ )(function* (organizationId: Organisation.OrganisationId) {
+ const customBucket = yield* repo.getForOrganization(organizationId);
+
+ return yield* Option.match(customBucket, {
+ onNone: () => Effect.succeed(Option.none()),
+ onSome: (bucket) =>
+ getBucketAccess(Option.some(bucket)).pipe(Effect.map(Option.some)),
+ });
+ }),
};
}),
dependencies: [
@@ -214,4 +225,10 @@ export class S3Buckets extends Effect.Service<S3Buckets>()("S3Buckets", {
);
static getBucketAccessForUser = (userId: User.UserId) =>
Effect.flatMap(S3Buckets, (b) => b.getBucketAccessForUser(userId));
+ static getBucketAccessForOrganization = (
+ organizationId: Organisation.OrganisationId,
+ ) =>
+ Effect.flatMap(S3Buckets, (b) =>
+ b.getBucketAccessForOrganization(organizationId),
+ );
}