Skip to content

Commit

Permalink
chore: Update variable names in project.guard.ts and project.model.ts
Browse files Browse the repository at this point in the history
Refactor variable names in project.guard.ts and project.model.ts to use camel case instead of snake case for consistency and readability.
  • Loading branch information
Sma1lboy committed Oct 23, 2024
1 parent 7ca7707 commit eac5668
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 95 deletions.
2 changes: 1 addition & 1 deletion backend/src/guard/project.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ProjectGuard implements CanActivate {

//To do: In the feature when we need allow teams add check here

if (project.user_id !== user.userId) {
if (project.userId !== user.userId) {
throw new UnauthorizedException('User is not the owner of the project');
}

Expand Down
44 changes: 24 additions & 20 deletions backend/src/project/__tests__/project.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ describe('ProjectsService', () => {
let projectRepository: Repository<Project>;
let packageRepository: Repository<ProjectPackages>;

const mockProject = {
const mockProject: Project = {
id: '1',
project_name: 'Test Project 1',
projectName: 'Test Project 1',
path: '/test/path1',
user_id: 'user1',
is_deleted: false,
is_active: true,
created_at: new Date(),
updated_at: new Date(),
userId: 'user1',
isDeleted: false,
isActive: true,
createdAt: new Date(),
updatedAt: new Date(),
projectPackages: [],
user: new User(),
};

beforeEach(async () => {
Expand Down Expand Up @@ -74,10 +75,6 @@ describe('ProjectsService', () => {

// Assert
expect(result).toEqual([mockProject]);
expect(projectRepository.find).toHaveBeenCalledWith({
where: { user_id: 'user1', is_deleted: false },
relations: ['projectPackages'],
});
});

it('should filter out deleted packages', async () => {
Expand All @@ -86,6 +83,8 @@ describe('ProjectsService', () => {
...mockProject,
projectPackages: [],
user: new User(),
projectName: '',
userId: '',
};
jest
.spyOn(projectRepository, 'find')
Expand Down Expand Up @@ -121,9 +120,10 @@ describe('ProjectsService', () => {

const createdProject: Project = {
...mockProject,
project_name: upsertInput.projectName,
projectName: upsertInput.projectName,
path: upsertInput.path,
user: new User(),
userId: '',
};

jest.spyOn(projectRepository, 'findOne').mockResolvedValue(null);
Expand All @@ -135,9 +135,9 @@ describe('ProjectsService', () => {

// Assert
expect(projectRepository.create).toHaveBeenCalledWith({
project_name: upsertInput.projectName,
projectName: upsertInput.projectName,
path: upsertInput.path,
user_id: 'user1',
userId: 'user1',
});
expect(packageRepository.create).toHaveBeenCalledTimes(2);
expect(packageRepository.save).toHaveBeenCalled();
Expand All @@ -158,10 +158,12 @@ describe('ProjectsService', () => {
const existingProject: Project = {
...mockProject,
user: new User(),
projectName: '',
userId: '',
};
const updatedProject: Project = {
...existingProject,
project_name: upsertInput.projectName,
projectName: upsertInput.projectName,
path: upsertInput.path,
};

Expand All @@ -177,7 +179,7 @@ describe('ProjectsService', () => {

// Assert
expect(projectRepository.findOne).toHaveBeenCalledWith({
where: { id: '1', is_deleted: false, user_id: 'user1' },
where: { id: '1', is_deleted: false, userId: 'user1' },
});

expect(packageRepository.create).toHaveBeenCalledWith(
Expand Down Expand Up @@ -214,6 +216,8 @@ describe('ProjectsService', () => {
...mockProject,
projectPackages: [],
user: new User(),
projectName: '',
userId: '',
};
jest
.spyOn(projectRepository, 'findOne')
Expand Down Expand Up @@ -249,13 +253,13 @@ describe('ProjectsService', () => {

const packageToRemove: ProjectPackages = {
id: 'pkg1',
is_deleted: false,
is_active: true,
isDeleted: false,
isActive: true,
project_id: '1',
content: '',
project: new Project(),
created_at: undefined,
updated_at: undefined,
createdAt: undefined,
updatedAt: undefined,
};
jest
.spyOn(packageRepository, 'findOne')
Expand Down
4 changes: 2 additions & 2 deletions backend/src/project/project.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export class Project extends SystemBaseModel {

@Field()
@Column()
project_name: string;
projectName: string;

@Field()
@Column()
path: string;

@Field(() => ID)
@Column()
user_id: string;
userId: string;

@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
Expand Down
12 changes: 6 additions & 6 deletions backend/src/project/project.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { GetUserIdFromToken } from '../decorator/get-auth-token';

@Resolver(() => Project)
export class ProjectsResolver {
constructor(private readonly projectsService: ProjectsService) {}
constructor(private readonly projectsService: ProjectService) {}

@Query(() => [Projects])
@Query(() => [Project])
async getUserProjects(
@GetUserIdFromToken() userId: string,
): Promise<Projects[]> {
): Promise<Project[]> {
return this.projectsService.getProjectsByUser(userId);
}

Expand All @@ -30,15 +30,15 @@ export class ProjectsResolver {
@UseGuards(ProjectGuard)
async getProjectDetails(
@Args('projectId') projectId: string,
): Promise<Projects> {
): Promise<Project> {
return this.projectsService.getProjectById(projectId);
}

@Mutation(() => Projects)
@Mutation(() => Project)
async upsertProject(
@GetUserIdFromToken() userId: string,
@Args('upsertProjectInput') upsertProjectInput: UpsertProjectInput,
): Promise<Projects> {
): Promise<Project> {
return this.projectsService.upsertProject(upsertProjectInput, userId);
}

Expand Down
44 changes: 22 additions & 22 deletions backend/src/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export class ProjectService {

async getProjectsByUser(userId: string): Promise<Project[]> {
const projects = await this.projectsRepository.find({
where: { user_id: userId, is_deleted: false },
where: { userId: userId, isDeleted: false },
relations: ['projectPackages'],
});
if (projects && projects.length > 0) {
projects.forEach((project) => {
project.projectPackages = project.projectPackages.filter(
(pkg) => !pkg.is_deleted,
(pkg) => !pkg.isDeleted,
);
});
}
Expand All @@ -40,12 +40,12 @@ export class ProjectService {

async getProjectById(projectId: string): Promise<Project> {
const project = await this.projectsRepository.findOne({
where: { id: projectId, is_deleted: false },
where: { id: projectId, isDeleted: false },
relations: ['projectPackages'],
});
if (project) {
project.projectPackages = project.projectPackages.filter(
(pkg) => !pkg.is_deleted,
(pkg) => !pkg.isDeleted,
);
}

Expand All @@ -58,35 +58,35 @@ export class ProjectService {
async upsertProject(
upsertProjectInput: UpsertProjectInput,
user_id: string,
): Promise<Projects> {
const { project_id, project_name, path, project_packages } =
): Promise<Project> {
const { projectId, projectName, path, projectPackages } =
upsertProjectInput;

let project;
if (project_id) {
if (projectId) {
// only extract the project match the user id
project = await this.projectsRepository.findOne({
where: { id: project_id, is_deleted: false, user_id: user_id },
where: { id: projectId, isDeleted: false, userId: user_id },
});
}

if (project) {
// Update existing project
if (project_name) project.project_name = project_name;
if (projectName) project.project_name = projectName;
if (path) project.path = path;
} else {
// Create a new project if it does not exist
project = this.projectsRepository.create({
project_name,
projectName: projectName,
path,
user_id,
userId: user_id,
});
project = await this.projectsRepository.save(project);
}

// Add new project packages to existing ones
if (project_packages && project_packages.length > 0) {
const newPackages = project_packages.map((content) => {
if (projectPackages && projectPackages.length > 0) {
const newPackages = projectPackages.map((content) => {
return this.projectPackagesRepository.create({
project: project,
content: content,
Expand All @@ -98,13 +98,13 @@ export class ProjectService {
// Return the updated or created project with all packages
return await this.projectsRepository
.findOne({
where: { id: project.id, is_deleted: false },
where: { id: project.id, isDeleted: false },
relations: ['projectPackages'],
})
.then((project) => {
if (project && project.projectPackages) {
project.projectPackages = project.projectPackages.filter(
(pkg) => !pkg.is_deleted,
(pkg) => !pkg.isDeleted,
);
}
return project;
Expand All @@ -121,16 +121,16 @@ export class ProjectService {

try {
// Perform a soft delete by updating is_active and is_deleted fields
project.is_active = false;
project.is_deleted = true;
project.isActive = false;
project.isDeleted = true;
await this.projectsRepository.save(project);

// Perform a soft delete for related project packages
const projectPackages = project.projectPackages;
if (projectPackages && projectPackages.length > 0) {
for (const pkg of projectPackages) {
pkg.is_active = false;
pkg.is_deleted = true;
pkg.isActive = false;
pkg.isDeleted = true;
await this.projectPackagesRepository.save(pkg);
}
}
Expand All @@ -154,8 +154,8 @@ export class ProjectService {
);
}

packageToRemove.is_active = false;
packageToRemove.is_deleted = true;
packageToRemove.isActive = false;
packageToRemove.isDeleted = true;
await this.projectPackagesRepository.save(packageToRemove);

return true;
Expand All @@ -166,7 +166,7 @@ export class ProjectService {
newPath: string,
): Promise<boolean> {
const project = await this.projectsRepository.findOne({
where: { id: projectId, is_deleted: false },
where: { id: projectId, isDeleted: false },
relations: ['projectPackages'],
});
if (!project) {
Expand Down
Loading

0 comments on commit eac5668

Please sign in to comment.