Skip to content

Commit

Permalink
Merge pull request #470 from COS301-SE-2023/dev/version_control_resource
Browse files Browse the repository at this point in the history
Dev/version control resource -> codecov
  • Loading branch information
JulianPienaar authored Sep 21, 2023
2 parents 61cc74f + 0185d84 commit 90c2e3c
Show file tree
Hide file tree
Showing 49 changed files with 2,813 additions and 281 deletions.
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"testTimeout": 30000
}
}
14 changes: 14 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import { TextractService } from './textract/textract.service';
import { ResetPasswordModule } from './reset_password/reset_password.module';
import { ResetPasswordRequest } from './reset_password/entities/reset_password_request.entity';
import { MailService } from './mail/mail.service';
import { DiffsService } from './diffs/diffs.service';
import { SnapshotService } from './snapshots/snapshots.service';
import { Diff } from './diffs/entities/diffs.entity';
import { Snapshot } from './snapshots/entities/snapshots.entity';
import { VersionControlModule } from './version_control/version_control.module';
import { VersionControlService } from './version_control/version_control.service';
import { VersionControlController } from './version_control/version_control.controller';

@Module({
imports: [
Expand All @@ -41,17 +48,21 @@ import { MailService } from './mail/mail.service';
TypeOrmModule.forFeature([
ResetPasswordRequest,
]),
TypeOrmModule.forFeature([Diff]),
TypeOrmModule.forFeature([Snapshot]),
MarkdownFilesModule,
FoldersModule,
S3Module,
FileManagerModule,
AssetManagerModule,
VersionControlModule,
ResetPasswordModule,
],
controllers: [
AuthController,
S3Controller,
FileManagerController,
VersionControlController
],
providers: [
FileManagerService,
Expand All @@ -62,6 +73,9 @@ import { MailService } from './mail/mail.service';
ConversionService,
TextractService,
MailService,
DiffsService,
SnapshotService,
VersionControlService,
],
})
export class AppModule {}
168 changes: 164 additions & 4 deletions backend/src/diffs/diffs.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,172 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Diff } from "./entities/diffs.entity";
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Diff } from './entities/diffs.entity';
import 'dotenv/config';
import * as CryptoJS from 'crypto-js';
import { MarkdownFileDTO } from '../markdown_files/dto/markdown_file.dto';
import { DiffDTO } from './dto/diffs.dto';

@Injectable()
export class DiffsService {
constructor(
@InjectRepository(Diff)
private diffRepository: Repository<Diff>,
) {}

///===-----------------------------------------------------

async getDiff(
diffDTO: DiffDTO,
nextDiffID: number,
) {
const diff =
await this.diffRepository.findOne({
where: {
MarkdownID: diffDTO.MarkdownID,
S3DiffID: nextDiffID,
},
});

return diff;
}

///===-----------------------------------------------------

async updateDiff(
diffDTO: DiffDTO,
nextDiffID: number,
) {
// const diff =
// await this.diffRepository.findOne({
// where: {
// MarkdownID: diffDTO.MarkdownID,
// S3DiffID: nextDiffID,
// },
// });

const diff = await this.getDiff(
diffDTO,
nextDiffID,
);

diff.LastModified = new Date();
diff.HasBeenUsed = true;
await this.diffRepository.save(diff);
}

///===-----------------------------------------------------

async createDiffs(
markdownFileDTO: MarkdownFileDTO,
snapshotIDs: string[],
) {
const diffRecords = [];
let snapshotIndex = 0;
for (
let i = 0;
i < parseInt(process.env.MAX_DIFFS);
i++
) {
const diffID = CryptoJS.SHA256(
markdownFileDTO.UserID.toString() +
new Date().getTime().toString() +
i.toString(),
).toString();

if (
i %
parseInt(
process.env.DIFFS_PER_SNAPSHOT,
) ===
0 &&
i !== 0
) {
snapshotIndex++;
}

diffRecords.push({
DiffID: diffID,
MarkdownID: markdownFileDTO.MarkdownID,
UserID: markdownFileDTO.UserID,
S3DiffID: i,
HasBeenUsed: false,
SnapshotID: snapshotIDs[snapshotIndex],
});
}
await this.diffRepository.insert(diffRecords);
}

///===-----------------------------------------------------

async deleteDiffs(
markdownFileDTO: MarkdownFileDTO,
) {
await this.diffRepository.delete({
MarkdownID: markdownFileDTO.MarkdownID,
});
}

///===-----------------------------------------------------

async resetDiffs(
markdownID: string,
snapshotID: string,
) {
await this.diffRepository.update(
{
MarkdownID: markdownID,
SnapshotID: snapshotID,
},
{
HasBeenUsed: false,
},
);
}

///===-----------------------------------------------------

async getAllDiffs(markdownID: string) {
return await this.diffRepository.find({
where: {
MarkdownID: markdownID,
HasBeenUsed: true,
},
});
}

///===-----------------------------------------------------

getLogicalIndex(
s3Index: number,
nextDiffID: number,
arr_len: number,
): number {
return (
(s3Index - nextDiffID + arr_len) % arr_len
);
}

///===-----------------------------------------------------

async getLogicalDiffOrder(
diffDTOs: DiffDTO[],
nextDiffID: number,
) {
const arrLength = parseInt(
process.env.MAX_DIFFS,
);
const logicalOrder: DiffDTO[] = new Array(
arrLength,
).fill(0);
for (let idx = 0; idx < arrLength; idx++) {
const logicalIndex = this.getLogicalIndex(
diffDTOs[idx].S3DiffID,
nextDiffID,
arrLength,
);
logicalOrder[logicalIndex] = diffDTOs[idx];
}
return logicalOrder;
}
}
23 changes: 23 additions & 0 deletions backend/src/diffs/dto/diffs.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export class DiffDTO {
DiffID: string;
MarkdownID: string;
UserID: number;
DisplayID: number;
S3DiffID: number;
LastModified: Date;
Content: string;
SnapshotPayload: string;
SnapshotID: string;

constructor() {
this.DiffID = undefined;
this.MarkdownID = undefined;
this.UserID = undefined;
this.DisplayID = undefined;
this.S3DiffID = undefined;
this.LastModified = undefined;
this.Content = undefined;
this.SnapshotPayload = undefined;
this.SnapshotID = undefined;
}
}
11 changes: 7 additions & 4 deletions backend/src/diffs/entities/diffs.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ export class Diff {
MarkdownID: string;

@Column()
UserID: string;

@Column()
DisplayID: string;
UserID: number;

@Column()
S3DiffID: number;
Expand All @@ -27,4 +24,10 @@ export class Diff {
onUpdate: 'CURRENT_TIMESTAMP(3)',
})
LastModified: Date;

@Column()
HasBeenUsed: boolean;

@Column()
SnapshotID: string;
}
20 changes: 20 additions & 0 deletions backend/src/file_manager/__mocks__/file_manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { ExportDTO } from '../dto/export.dto';
import * as CryptoJS from 'crypto-js';
import { ConversionService } from '../../conversion/conversion.service';
import { ImportDTO } from '../dto/import.dto';
import { DiffsService } from '../../diffs/diffs.service';
import { SnapshotService } from '../../snapshots/snapshots.service';

@Injectable()
export class FileManagerServiceMock {
Expand All @@ -26,6 +28,8 @@ export class FileManagerServiceMock {
private s3service: S3Service,
private conversionService: ConversionService,
private userService: UsersService,
private diffsService: DiffsService,
private snapshotService: SnapshotService,
) {}

// File operations: ###########################################################
Expand Down Expand Up @@ -233,6 +237,22 @@ export class FileManagerServiceMock {
markdownFileDTO,
);

await this.s3service.deleteDiffObjectsForFile(
markdownFileDTO,
);

await this.s3service.deleteSnapshotObjectsForFile(
markdownFileDTO,
);

await this.diffsService.deleteDiffs(
markdownFileDTO,
);

await this.snapshotService.deleteSnapshots(
markdownFileDTO,
);

return this.markdownFilesService.remove(
markdownFileDTO,
);
Expand Down
14 changes: 14 additions & 0 deletions backend/src/file_manager/file_manager.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import { JwtService } from '@nestjs/jwt';
import { ResetPasswordService } from '../reset_password/reset_password.service';
import { ResetPasswordRequest } from '../reset_password/entities/reset_password_request.entity';
import { MailService } from '../mail/mail.service';
import { DiffsService } from '../diffs/diffs.service';
import { SnapshotService } from '../snapshots/snapshots.service';
import { Diff } from '../diffs/entities/diffs.entity';
import { Snapshot } from '../snapshots/entities/snapshots.entity';

describe('FileManagerController', () => {
let controller: FileManagerController;
Expand All @@ -54,6 +58,8 @@ describe('FileManagerController', () => {
JwtService,
ResetPasswordService,
MailService,
DiffsService,
SnapshotService,
{
provide: 'FileManagerService',
useValue: {
Expand All @@ -74,6 +80,14 @@ describe('FileManagerController', () => {
provide: getRepositoryToken(User),
useClass: Repository,
},
{
provide: getRepositoryToken(Diff),
useClass: Repository,
},
{
provide: getRepositoryToken(Snapshot),
useClass: Repository,
},
{
provide: getRepositoryToken(
ResetPasswordRequest,
Expand Down
8 changes: 8 additions & 0 deletions backend/src/file_manager/file_manager.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { S3ServiceMock } from '../s3/__mocks__/s3.service';
import { ResetPasswordService } from '../reset_password/reset_password.service';
import { ResetPasswordRequest } from '../reset_password/entities/reset_password_request.entity';
import { MailService } from '../mail/mail.service';
import { DiffsService } from '../diffs/diffs.service';
import { SnapshotService } from '../snapshots/snapshots.service';
import { Diff } from '../diffs/entities/diffs.entity';
import { Snapshot } from '../snapshots/entities/snapshots.entity';

@Module({
imports: [
Expand All @@ -24,6 +28,8 @@ import { MailService } from '../mail/mail.service';
TypeOrmModule.forFeature([
ResetPasswordRequest,
]),
TypeOrmModule.forFeature([Diff]),
TypeOrmModule.forFeature([Snapshot]),
],
controllers: [FileManagerController],
providers: [
Expand All @@ -37,6 +43,8 @@ import { MailService } from '../mail/mail.service';
S3ServiceMock,
ResetPasswordService,
MailService,
DiffsService,
SnapshotService,
],
})
export class FileManagerModule {}
Loading

0 comments on commit 90c2e3c

Please sign in to comment.