Skip to content

Commit

Permalink
feat(user): create jobs to process user actions on posts
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrdsilva committed Jun 9, 2024
1 parent 8936951 commit 8bd5a0a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/modules/post.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { User } from 'src/entities/user.entity';
import { PostService } from 'src/providers/post.service';
import { StorageClientService } from 'src/providers/storage-client.service';
import { PostResolver } from 'src/resolvers/post.resolver';
import { UserModule } from './user.module';

@Module({
imports: [TypeOrmModule.forFeature([Post, User])],
imports: [TypeOrmModule.forFeature([Post, User]), UserModule],
controllers: [PostController],
providers: [PostResolver, PostService, StorageClientService],
exports: [PostService],
Expand Down
40 changes: 37 additions & 3 deletions src/providers/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import { Repository } from 'typeorm';
import { CreatePostInput } from '../models/dtos/create-post.input';
import { UpdatePostInput } from '../models/dtos/update-post.input';
import { StorageClientService } from './storage-client.service';
import { UserService } from './user.service';

@Injectable()
export class PostService {
constructor(
@InjectRepository(Post)
private readonly postRepository: Repository<Post>,
private readonly storageClientService: StorageClientService,
private readonly userService: UserService,
) {}

private readonly logger = new Logger(PostService.name);
Expand All @@ -46,6 +48,11 @@ export class PostService {
post.user = user;
post.files = savedFiles;

await this.userService.createJob({
user: user,
description: `${user.firstName} created a post.`,
});

return await this.postRepository.save(post);
}

Expand Down Expand Up @@ -91,8 +98,17 @@ export class PostService {

if (!post.likes.some((like) => like.id === user.id)) {
post.likes.push(user);

await this.postRepository.save(post);
return (await this.postRepository.save(post)).id;

const likedPostId = (await this.postRepository.save(post)).id;

await this.userService.createJob({
user: user,
description: `${user.firstName} liked a post.`,
});

return likedPostId;
}

return "It seems like you've liked this post already 🙃";
Expand All @@ -104,7 +120,14 @@ export class PostService {

post.likes = post.likes.filter((like) => like.id !== user.id);

return (await this.postRepository.save(post)).id;
const unlikedPostId = (await this.postRepository.save(post)).id;

await this.userService.createJob({
user: user,
description: `${user.firstName} unliked a post.`,
});

return unlikedPostId;
}

async findById(postId: string) {
Expand Down Expand Up @@ -146,7 +169,14 @@ export class PostService {

post.postText = updatePostInput.postText;

return await this.postRepository.save(post);
const updatedPost = await this.postRepository.save(post);

await this.userService.createJob({
user: user,
description: `${user.firstName} updated their post.`,
});

return updatedPost;
}

async remove(request: Request, id: string) {
Expand All @@ -168,6 +198,10 @@ export class PostService {
}

await this.postRepository.remove(post);
await this.userService.createJob({
user: user,
description: `${user.firstName} removed their post.`,
});

return { message: 'Post deleted successfully!' };
}
Expand Down
4 changes: 2 additions & 2 deletions src/providers/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class UserService {

await this.createJob({
user: user,
description: `${user.firstName} changed their visibility.`,
description: `${user.firstName} changed their visibility settings.`,
});

return this.userRepository.create({ ...user, isPrivate: isPrivate });
Expand All @@ -179,7 +179,7 @@ export class UserService {

await this.createJob({
user: user,
description: `${user.firstName} changed their role.`,
description: `${user.firstName}'s role was updated.`,
});

return { message: 'Role updated successfully!' };
Expand Down

0 comments on commit 8bd5a0a

Please sign in to comment.