| 70 | }; |
| 71 | |
| 72 | export class StorageRepo extends Effect.Service<StorageRepo>()("StorageRepo", { |
| 73 | effect: Effect.gen(function* () { |
| 74 | const db = yield* Database; |
| 75 | |
| 76 | const decodeGoogleDriveAccessTokenCache = Effect.fn( |
| 77 | "StorageRepo.decodeGoogleDriveAccessTokenCache", |
| 78 | )( |
| 79 | (input: { |
| 80 | googleDriveAccessToken: string | null; |
| 81 | googleDriveAccessTokenExpiresAt: Date | null; |
| 82 | }) => |
| 83 | Effect.gen(function* () { |
| 84 | if ( |
| 85 | !input.googleDriveAccessToken || |
| 86 | !input.googleDriveAccessTokenExpiresAt |
| 87 | ) { |
| 88 | return Option.none<GoogleDriveAccessTokenCache>(); |
| 89 | } |
| 90 | |
| 91 | const accessToken = yield* Effect.tryPromise({ |
| 92 | try: () => decrypt(input.googleDriveAccessToken as string), |
| 93 | catch: (cause) => new Storage.StorageError({ cause }), |
| 94 | }); |
| 95 | |
| 96 | return Option.some({ |
| 97 | accessToken, |
| 98 | expiresAt: input.googleDriveAccessTokenExpiresAt, |
| 99 | }); |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | const getActiveIntegrationForUser = Effect.fn( |
| 104 | "StorageRepo.getActiveIntegrationForUser", |
| 105 | )((userId: User.UserId) => |
| 106 | Effect.gen(function* () { |
| 107 | const [integration] = yield* db.use((db) => |
| 108 | db |
| 109 | .select() |
| 110 | .from(Db.storageIntegrations) |
| 111 | .where( |
| 112 | Dz.and( |
| 113 | Dz.eq(Db.storageIntegrations.ownerId, userId), |
| 114 | Dz.isNull(Db.storageIntegrations.organizationId), |
| 115 | Dz.eq(Db.storageIntegrations.active, true), |
| 116 | Dz.eq(Db.storageIntegrations.status, "active"), |
| 117 | ), |
| 118 | ), |
| 119 | ); |
| 120 | |
| 121 | return Option.fromNullable(integration); |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | const getActiveIntegrationForOrganization = Effect.fn( |
| 126 | "StorageRepo.getActiveIntegrationForOrganization", |
| 127 | )((organizationId: Organisation.OrganisationId) => |
| 128 | Effect.gen(function* () { |
| 129 | const [integration] = yield* db.use((db) => |
nothing calls this directly
no test coverage detected