Skip to content

Commit

Permalink
Merge pull request #160 from plezanje-net/delete-user-mutation
Browse files Browse the repository at this point in the history
Delete user mutation
  • Loading branch information
salamca authored Jun 4, 2023
2 parents 79be106 + 431b3eb commit 831c01d
Show file tree
Hide file tree
Showing 14 changed files with 988 additions and 114 deletions.
17 changes: 6 additions & 11 deletions src/activities/activities.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuditModule } from '../audit/audit.module';
import { Crag } from '../crags/entities/crag.entity';
Expand All @@ -16,13 +16,10 @@ import { Club } from '../users/entities/club.entity';
import { DifficultyVote } from '../crags/entities/difficulty-vote.entity';
import { ActivityLoader } from './loaders/activity.loader';
import { StarRatingVote } from '../crags/entities/star-rating-vote.entity';
import { RouteLoader } from '../crags/loaders/route.loader';
import { CragLoader } from '../crags/loaders/crag.loader';
import { RoutesService } from '../crags/services/routes.service';
import { CragsService } from '../crags/services/crags.service';
import { Sector } from '../crags/entities/sector.entity';
import { Country } from '../crags/entities/country.entity';
import { BullModule } from '@nestjs/bull';
import { CragsModule } from '../crags/crags.module';

@Module({
imports: [
Expand All @@ -39,22 +36,20 @@ import { BullModule } from '@nestjs/bull';
Sector,
Country,
]),
AuditModule,
UsersModule,
forwardRef(() => AuditModule), // because audit module is imported in usersmodule and this constitutes a circular dependency (users->audit->activities->users...)
forwardRef(() => UsersModule),
BullModule.registerQueue({
name: 'summary',
}),
CragsModule,
],
providers: [
ActivitiesResolver,
ActivitiesService,
ActivityRoutesResolver,
ActivityRoutesService,
ActivityLoader,
RoutesService,
CragsService,
RouteLoader,
CragLoader,
],
exports: [ActivitiesService],
})
export class ActivitiesModule {}
1 change: 0 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ import { BullModule } from '@nestjs/bull';
AuditModule,
UsersModule,
CragsModule,
AuditModule,
NotificationModule,
ActivitiesModule,
BullModule.forRootAsync({
Expand Down
14 changes: 5 additions & 9 deletions src/audit/audit.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ import { ContributionsService } from './services/contributions.service';
import { Route } from '../crags/entities/route.entity';
import { Sector } from '../crags/entities/sector.entity';
import { Crag } from '../crags/entities/crag.entity';
import { UsersService } from '../users/services/users.service';
import { User } from '../users/entities/user.entity';
import { Role } from '../users/entities/role.entity';
import { CragsService } from '../crags/services/crags.service';
import { SectorsService } from '../crags/services/sectors.service';
import { RoutesService } from '../crags/services/routes.service';
import { Country } from '../crags/entities/country.entity';
import { Area } from '../crags/entities/area.entity';
import { GradingSystem } from '../crags/entities/grading-system.entity';
import { DifficultyVote } from '../crags/entities/difficulty-vote.entity';
import { Activity } from '../activities/entities/activity.entity';
import { ActivityRoute } from '../activities/entities/activity-route.entity';
import { BullModule } from '@nestjs/bull';
import { UsersModule } from '../users/users.module';
import { CragsModule } from '../crags/crags.module';

@Module({
imports: [
Expand All @@ -40,16 +38,14 @@ import { BullModule } from '@nestjs/bull';
GradingSystem,
DifficultyVote,
]),
forwardRef(() => AuthModule),
AuthModule,
BullModule.registerQueue({
name: 'summary',
}),
forwardRef(() => UsersModule),
forwardRef(() => CragsModule),
],
providers: [
UsersService,
CragsService,
SectorsService,
RoutesService,
AuditService,
AuditSubscriber,
AuditInterceptor,
Expand Down
19 changes: 17 additions & 2 deletions src/crags/crags.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuditModule } from '../audit/audit.module';

Expand Down Expand Up @@ -65,6 +65,9 @@ import { Activity } from '../activities/entities/activity.entity';
import { ActivityRoute } from '../activities/entities/activity-route.entity';
import { BullModule } from '@nestjs/bull';
import { SummaryQueueConsumer } from './consumers/summary-queue.consumer';
import { CragLoader } from './loaders/crag.loader';
import { RouteLoader } from './loaders/route.loader';
import { RouteEvent } from './entities/route-event.entity';

@Module({
imports: [
Expand All @@ -86,12 +89,13 @@ import { SummaryQueueConsumer } from './consumers/summary-queue.consumer';
GradingSystem,
RouteType,
DifficultyVote,
RouteEvent,
RouteProperty,
CragProperty,
IceFallProperty,
StarRatingVote,
]),
AuditModule,
forwardRef(() => AuditModule),
BullModule.registerQueue({
name: 'summary',
}),
Expand Down Expand Up @@ -136,7 +140,18 @@ import { SummaryQueueConsumer } from './consumers/summary-queue.consumer';
MailService,
SummaryQueueConsumer,
ConfigService,
CragLoader,
RouteLoader,
],
controllers: [UploadController],
exports: [
CragsService,
SectorsService,
RoutesService,
CragLoader,
RouteLoader,
CommentsService,
ImagesService,
],
})
export class CragsModule {}
9 changes: 9 additions & 0 deletions src/crags/services/comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IceFall } from '../entities/ice-fall.entity';
import { PaginationMeta } from '../../core/utils/pagination-meta.class';
import { PaginatedComments } from '../utils/paginated-comments';
import { LatestCommentsInput } from '../dtos/latest-comments.input';
import { Peak } from '../entities/peak.entity';

@Injectable()
export class CommentsService {
Expand All @@ -21,6 +22,8 @@ export class CommentsService {
private cragRepository: Repository<Crag>,
@InjectRepository(IceFall)
private iceFallRepository: Repository<IceFall>,
@InjectRepository(Peak)
private peakRepository: Repository<Peak>,
) {}

async findOneById(id: string): Promise<Comment> {
Expand Down Expand Up @@ -48,6 +51,12 @@ export class CommentsService {
);
}

if (data.peakId != null) {
comment.peak = Promise.resolve(
await this.peakRepository.findOneByOrFail({ id: data.peakId }),
);
}

return this.commentsRepository.save(comment);
}

Expand Down
56 changes: 19 additions & 37 deletions src/crags/services/images.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,39 +111,25 @@ export class ImagesService {
}

async deleteImage(id: string): Promise<Boolean> {
try {
const image = await this.imagesRepository.findOneOrFail({
where: {
id,
},
});

this.targetSizes.forEach((size) => {
fs.rm(
`${env.STORAGE_PATH}/images/${image.path}.jpg`,
this.handleImageRemove,
);
fs.rm(
`${env.STORAGE_PATH}/images/${size}/${image.path}.webp`,
this.handleImageRemove,
);
fs.rm(
`${env.STORAGE_PATH}/images/${size}/${image.path}.avif`,
this.handleImageRemove,
);
fs.rm(
`${env.STORAGE_PATH}/images/${size}/${image.path}.jpg`,
this.handleImageRemove,
);
});

await this.imagesRepository.remove(image);

return true;
} catch (error) {
// TODO log to Sentry when we have it on the API
return false;
}
const image = await this.imagesRepository.findOneOrFail({
where: {
id,
},
});

// delete the 'master' image file
fs.unlinkSync(`${env.STORAGE_PATH}/images/${image.path}${image.extension}`);

// delete all the size/format variations of the image file
this.targetSizes.forEach((size) => {
fs.unlinkSync(`${env.STORAGE_PATH}/images/${size}/${image.path}.webp`);
fs.unlinkSync(`${env.STORAGE_PATH}/images/${size}/${image.path}.avif`);
fs.unlinkSync(`${env.STORAGE_PATH}/images/${size}/${image.path}.jpg`);
});

await this.imagesRepository.remove(image);

return true;
}

private async generateUniqueStem(entity: string, stemBase: string) {
Expand Down Expand Up @@ -224,8 +210,4 @@ export class ImagesService {

return { maxIntrinsicWidth, aspectRatio };
}

private handleImageRemove() {
// TODO log to Sentry when we have it on the API
}
}
39 changes: 37 additions & 2 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import { Role } from './role.entity';
import { Image } from '../../crags/entities/image.entity';
import { ClubMember } from './club-member.entity';
import { checkRoleMiddleware } from '../../core/middleware/check-role.middleware';
import { Activity } from '../../activities/entities/activity.entity';
import { Comment } from '../../crags/entities/comment.entity';
import { Crag } from '../../crags/entities/crag.entity';
import { Sector } from '../../crags/entities/sector.entity';
import { Route } from '../../crags/entities/route.entity';
import { RouteEvent } from '../../crags/entities/route-event.entity';
import { DifficultyVote } from '../../crags/entities/difficulty-vote.entity';

@Entity()
@ObjectType()
Expand Down Expand Up @@ -80,9 +87,9 @@ export class User extends BaseEntity {
legacy: string;

@OneToMany((type) => ClubMember, (clubMember) => clubMember.user)
clubs: ClubMember[];
clubs: Promise<ClubMember[]>;

// All of thr images that the user contributed
// All of the images that the user contributed
@OneToMany(() => Image, (image) => image.user)
@Field(() => [Image])
images: Promise<Image[]>;
Expand All @@ -97,4 +104,32 @@ export class User extends BaseEntity {
const roles = await this.roles;
return roles ? roles.some((r) => r.role == 'admin') : false;
};

@OneToMany(() => Activity, (activity) => activity.user)
@Field(() => [Activity])
activities: Promise<Activity[]>;

@OneToMany(() => Comment, (comment) => comment.user)
@Field(() => [Comment])
comments: Promise<Comment[]>;

@OneToMany(() => Crag, (crag) => crag.user)
@Field(() => [Crag])
crags: Promise<Crag[]>;

@OneToMany(() => Sector, (sector) => sector.user)
@Field(() => [Sector])
sectors: Promise<Sector[]>;

@OneToMany(() => Route, (route) => route.user)
@Field(() => [Route])
routes: Promise<Route[]>;

@OneToMany(() => RouteEvent, (routeEvent) => routeEvent.user)
@Field(() => [RouteEvent])
routeEvents: Promise<RouteEvent[]>;

@OneToMany(() => DifficultyVote, (difficultyVote) => difficultyVote.user)
@Field(() => [DifficultyVote])
difficultyVotes: Promise<DifficultyVote[]>;
}
14 changes: 7 additions & 7 deletions src/users/resolvers/club-members.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { Club } from '../entities/club.entity';
import { User } from '../entities/user.entity';
import { ClubMembersService } from '../services/club-members.service';

@Resolver(of => ClubMember)
@Resolver((of) => ClubMember)
export class ClubMembersResolver {
constructor(
private clubMembersService: ClubMembersService,
private notificationService: NotificationService,
) {}

@UseGuards(UserAuthGuard)
@Mutation(returns => ClubMember)
@Mutation((returns) => ClubMember)
async createClubMember(
@CurrentUser() user: User,
@Args('input', { type: () => CreateClubMemberInput })
Expand All @@ -29,7 +29,7 @@ export class ClubMembersResolver {
}

@UseGuards(UserAuthGuard)
@Mutation(returns => ClubMember)
@Mutation((returns) => ClubMember)
async createClubMemberByEmail(
@CurrentUser() user: User,
@Args('input', { type: () => CreateClubMemberByEmailInput })
Expand All @@ -52,19 +52,19 @@ export class ClubMembersResolver {
return Promise.resolve(clubMember);
}

@Mutation(returns => Club)
@Mutation((returns) => Club)
async confirmClubMembership(
@Args('input', { type: () => ConfirmInput }) input: ConfirmInput,
): Promise<Club> {
return this.clubMembersService.confirmClubMembership(input);
}

@UseGuards(UserAuthGuard)
@Mutation(returns => Boolean)
@Mutation((returns) => Boolean)
async deleteClubMember(
@CurrentUser() user: User,
@CurrentUser() currentUser: User,
@Args('id') id: string,
): Promise<boolean> {
return this.clubMembersService.delete(user, id);
return this.clubMembersService.delete(currentUser, id);
}
}
Loading

0 comments on commit 831c01d

Please sign in to comment.