Skip to content

Commit

Permalink
Merge pull request #152 from internxt/feat/break-email-dependency
Browse files Browse the repository at this point in the history
[PB-1142]: feat/break-email-dependency
  • Loading branch information
sg-gs authored Nov 22, 2023
2 parents 86bec5c + 7f5e87f commit ce9fa66
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 120 deletions.
14 changes: 7 additions & 7 deletions lib/core/bucketEntries/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ export class BucketEntriesUsecase {
return count;
}

async removeFileFromUser(bucketId: string, fileId: string, userId: string) {
async removeFileFromUser(bucketId: string, fileId: string, userId: User['uuid']) {
const bucket = await this.bucketsRepository.findOne({ id: bucketId });

if(!bucket) {
throw new BucketNotFoundError();
}

if (bucket.user !== userId) {
if (bucket.userId !== userId) {
throw new BucketForbiddenError();
}

Expand Down Expand Up @@ -86,11 +86,11 @@ export class BucketEntriesUsecase {
await this.removeFilesV2([ bucketEntry ]);
const bucket = await this.bucketsRepository.findOne({ id: bucketEntry.bucket });

if (bucket?.user) {
const user = await this.usersRepository.findById(bucket.user);
if (bucket?.userId) {
const user = await this.usersRepository.findByUuid(bucket.userId);

if (user) {
await this.usersRepository.addTotalUsedSpaceBytes(bucket.user, - bucketEntry.size!);
await this.usersRepository.addTotalUsedSpaceBytes(user.uuid, - bucketEntry.size!);
}
}
} else {
Expand All @@ -113,8 +113,8 @@ export class BucketEntriesUsecase {
const bucketEntriesGroupedByBucket = lodash.groupBy(bucketEntriesV2, (b) => b.bucket);
const buckets = await this.bucketsRepository.findByIds(Object.keys(bucketEntriesGroupedByBucket));

const bucketsGroupedByUsers = lodash.groupBy(buckets, (b) => b.user);
const storageToModifyPerUser: Record<User['id'], number> = {};
const bucketsGroupedByUsers = lodash.groupBy(buckets, (b) => b.userId);
const storageToModifyPerUser: Record<User['uuid'], number> = {};

Object.keys(bucketsGroupedByUsers).map((userId) => {
storageToModifyPerUser[userId] = 0;
Expand Down
6 changes: 3 additions & 3 deletions lib/core/buckets/MongoDBBucketsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class MongoDBBucketsRepository implements BucketsRepository {
}

async findByUser(userId: string, limit: number, skip: number): Promise<Bucket[]> {
const buckets = await this.model.find({ user: userId }).skip(skip).limit(limit).exec();
const buckets = await this.model.find({ userId }).skip(skip).limit(limit).exec();

return buckets;
}
Expand All @@ -48,9 +48,9 @@ export class MongoDBBucketsRepository implements BucketsRepository {
return formatFromMongoToBucket(rawModel);
}

destroyByUser(userId: string): Promise<void> {
destroyByUser(userId: Bucket['userId']): Promise<void> {
return this.model.deleteMany({
user: userId,
userId,
});
}

Expand Down
5 changes: 2 additions & 3 deletions lib/core/buckets/Repository.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { User } from '../users/User';
import { Bucket } from './Bucket';

export interface BucketsRepository {
findOne(where: Partial<Bucket>): Promise<Bucket | null>;
findByUser(userId: User['id'], limit: number, skip: number): Promise<Bucket[]>;
findByUser(userId: Bucket['userId'], limit: number, skip: number): Promise<Bucket[]>;
findByIds(ids: Bucket['id'][]): Promise<Bucket[]>;
find(where: Partial<Bucket>): Promise<Bucket[]>;
destroyByUser(userId: User['id']): Promise<void>;
destroyByUser(userId: Bucket['userId']): Promise<void>;
removeAll(where: Partial<Bucket>): Promise<void>;
}
28 changes: 16 additions & 12 deletions lib/core/buckets/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class BucketsUsecase {
}

async startUpload(
userId: string,
userId: User['uuid'],
bucketId: string,
cluster: string[],
uploads: { index: number; size: number }[],
Expand All @@ -284,7 +284,7 @@ export class BucketsUsecase {
) {
const [bucket, user] = await Promise.all([
this.bucketsRepository.findOne({ id: bucketId }),
this.usersRepository.findById(userId),
this.usersRepository.findByUuid(userId),
]);

if (!user) {
Expand All @@ -295,7 +295,9 @@ export class BucketsUsecase {
throw new BucketNotFoundError();
}

if (bucket.user !== userId) {
const isBucketOwnedByUser = bucket.userId === user.uuid;

if (!isBucketOwnedByUser) {
throw new BucketForbiddenError();
}

Expand All @@ -318,7 +320,7 @@ export class BucketsUsecase {
throw new MaxSpaceUsedError();
}
} else {
const usedSpaceBytes = await this.getUserUsage(user.id);
const usedSpaceBytes = await this.getUserUsage(user.email);

if (
user.maxSpaceBytes <
Expand Down Expand Up @@ -418,15 +420,15 @@ export class BucketsUsecase {
}

async completeUpload(
userId: string,
bucketId: string,
userId: User['uuid'],
bucketId: Bucket['id'],
fileIndex: string,
shards: ShardWithPossibleMultiUpload[],
auth: { username: string; password: string }
): Promise<BucketEntry> {
const [bucket, user, uploads] = await Promise.all([
this.bucketsRepository.findOne({ id: bucketId }),
this.usersRepository.findById(userId),
this.usersRepository.findByUuid(userId),
this.uploadsRepository.findByUuids(shards.map((s) => s.uuid)),
]);

Expand All @@ -438,7 +440,9 @@ export class BucketsUsecase {
throw new BucketNotFoundError();
}

if (bucket.user !== userId) {
const isBucketOwnedByUser = bucket.userId === user.uuid;

if (!isBucketOwnedByUser) {
throw new BucketForbiddenError();
}

Expand All @@ -465,7 +469,7 @@ export class BucketsUsecase {
throw new MaxSpaceUsedError();
}
} else {
const usedSpaceBytes = await this.getUserUsage(user.id);
const usedSpaceBytes = await this.getUserUsage(user.email);

if (
user.maxSpaceBytes <
Expand Down Expand Up @@ -514,7 +518,7 @@ export class BucketsUsecase {
});

await this.bucketEntryShardsRepository.insertMany(bucketEntryShards);
await this.usersRepository.addTotalUsedSpaceBytes(userId, bucketEntrySize);
await this.usersRepository.addTotalUsedSpaceBytes(user.uuid, bucketEntrySize);
this.uploadsRepository
.deleteManyByUuids(uploads.map((u) => u.uuid))
.catch((err) => {
Expand Down Expand Up @@ -651,13 +655,13 @@ export class BucketsUsecase {
return newShard;
}

async listByUserId(userId: User['id'], limit: number, offset: number): Promise<Bucket[]> {
async listByUserId(userId: User['uuid'], limit: number, offset: number): Promise<Bucket[]> {
const buckets = await this.bucketsRepository.findByUser(userId, limit, offset);

return buckets;
}

async destroyByUser(userId: User['id']) {
async destroyByUser(userId: User['uuid']) {
await this.bucketsRepository.destroyByUser(userId);
}
}
1 change: 0 additions & 1 deletion lib/core/frames/MongoDBFramesRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { User } from '../users/User';
import { Frame } from './Frame';
import { FramesRepository } from './Repository';

Expand Down
10 changes: 8 additions & 2 deletions lib/core/users/MongoDBUsersRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export class MongoDBUsersRepository implements UsersRepository {
return users.map(formatFromMongoToUser);
}

async findByUuid(uuid: string): Promise<User | null> {
const user = await this.userModel.findOne({ uuid });

return user ? formatFromMongoToUser(user) : null;
}

async findOne(where: Partial<User>): Promise<BasicUser | null> {
const user: DatabaseUser = await this.userModel.findOne(where);

Expand Down Expand Up @@ -116,11 +122,11 @@ export class MongoDBUsersRepository implements UsersRepository {
}

addTotalUsedSpaceBytes(
id: string,
uuid: string,
totalUsedSpaceBytes: number
): Promise<void> {
return this.userModel.updateOne(
{ _id: id },
{ uuid },
{ $inc: { totalUsedSpaceBytes } }
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/core/users/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { BasicUser, CreateUserData, User } from "./User";

export interface UsersRepository {
findById(id: User['id']): Promise<User | null>;
findByUuid(uuid: User['uuid']): Promise<User | null>;
findOne(where: Partial<User>): Promise<BasicUser | null>;
findByEmail(email: User['email']): Promise<User | null>;
findByIds(ids: User['id'][]): Promise<User[]>;
create(data: CreateUserData): Promise<BasicUser>;
addTotalUsedSpaceBytes(id: User['id'], totalUsedSpaceBytes: number): Promise<void>;
addTotalUsedSpaceBytes(uuid: User['uuid'], totalUsedSpaceBytes: User['totalUsedSpaceBytes']): Promise<void>;
updateById(id: User['id'], update: Partial<User>): Promise<User | null>;
updateByEmail(email: User['email'], update: Partial<User>): Promise<User | null>;
updateByUuid(uuid: User['uuid'], update: Partial<User>): Promise<BasicUser | null>;
Expand Down
6 changes: 3 additions & 3 deletions lib/core/users/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class UsersUsecase {
) {}

async findOrCreateUser(email: string, password: string): Promise<BasicUser> {
const user = await this.usersRepository.findById(email);
const user = await this.usersRepository.findByEmail(email);

if (user) {
const newHassPass = createHash('sha256').update(password).digest('hex');
Expand Down Expand Up @@ -219,8 +219,8 @@ export class UsersUsecase {
}

await Promise.all([
this.bucketsRepository.removeAll({ user: user.id }),
this.framesRepository.removeAll({ user: user.id })
this.bucketsRepository.removeAll({ userId: user.uuid }),
this.framesRepository.removeAll({ user: user.email })
]);

await this.usersRepository.removeById(user.id);
Expand Down
Loading

0 comments on commit ce9fa66

Please sign in to comment.