Skip to content

Commit

Permalink
feat(server): add the first notification on createUser in `UserServ…
Browse files Browse the repository at this point in the history
…ice`

This trigger an event for the `NotificationRepository` that once processes
by using the global config and per-user config will carry the payload to the right notification transport.
  • Loading branch information
hitech95 committed Apr 4, 2024
1 parent 9b441ce commit ccfbfe0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion server/src/services/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { newLibraryRepositoryMock } from 'test/repositories/library.repository.m
import { newStorageRepositoryMock } from 'test/repositories/storage.repository.mock';
import { newSystemConfigRepositoryMock } from 'test/repositories/system-config.repository.mock';
import { newUserRepositoryMock } from 'test/repositories/user.repository.mock';
import {INotificationRepository} from "../interfaces/notification.interface";
import {newNotificationRepositoryMock} from "../../test/repositories/notification.repository.mock";

const makeDeletedAt = (daysAgo: number) => {
const deletedAt = new Date();
Expand All @@ -43,6 +45,7 @@ describe(UserService.name, () => {
let libraryMock: jest.Mocked<ILibraryRepository>;
let storageMock: jest.Mocked<IStorageRepository>;
let configMock: jest.Mocked<ISystemConfigRepository>;
let notificationMock: jest.Mocked<INotificationRepository>;

beforeEach(() => {
albumMock = newAlbumRepositoryMock();
Expand All @@ -52,8 +55,9 @@ describe(UserService.name, () => {
libraryMock = newLibraryRepositoryMock();
storageMock = newStorageRepositoryMock();
userMock = newUserRepositoryMock();
notificationMock = newNotificationRepositoryMock();

sut = new UserService(albumMock, cryptoRepositoryMock, jobMock, libraryMock, storageMock, configMock, userMock);
sut = new UserService(albumMock, cryptoRepositoryMock, jobMock, libraryMock, storageMock, configMock, userMock, notificationMock);

when(userMock.get).calledWith(authStub.admin.user.id, {}).mockResolvedValue(userStub.admin);
when(userMock.get).calledWith(authStub.admin.user.id, { withDeleted: true }).mockResolvedValue(userStub.admin);
Expand Down
18 changes: 17 additions & 1 deletion server/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { IAlbumRepository } from 'src/interfaces/album.interface';
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
import { IEntityJob, IJobRepository, JobName, JobStatus } from 'src/interfaces/job.interface';
import { ILibraryRepository } from 'src/interfaces/library.interface';
import {
INotificationRepository,
NotificationName,
UserCreatedNotification,
} from 'src/interfaces/notification.interface';
import { IStorageRepository } from 'src/interfaces/storage.interface';
import { ISystemConfigRepository } from 'src/interfaces/system-config.interface';
import { IUserRepository, UserFindOptions } from 'src/interfaces/user.interface';
Expand All @@ -31,6 +36,7 @@ export class UserService {
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
@Inject(ISystemConfigRepository) configRepository: ISystemConfigRepository,
@Inject(IUserRepository) private userRepository: IUserRepository,
@Inject(INotificationRepository) private notificationRepository: INotificationRepository,
) {
this.userCore = UserCore.create(cryptoRepository, libraryRepository, userRepository);
this.configCore = SystemConfigCore.create(configRepository);
Expand All @@ -55,7 +61,17 @@ export class UserService {
}

create(createUserDto: CreateUserDto): Promise<UserResponseDto> {
return this.userCore.createUser(createUserDto).then(mapUser);
return this.userCore.createUser(createUserDto).then((userEntity: UserEntity) => {
const userDto = mapUser(userEntity);

// QUESTION: Should we do this here or in `user.core.ts`?
return this.notificationRepository
.notify<UserCreatedNotification>(NotificationName.NOTIFY_USER_INVITE, {
user: userDto,
tempPassword: userEntity.shouldChangePassword ? createUserDto.password : undefined,
})
.then(() => userDto);
});
}

async update(auth: AuthDto, dto: UpdateUserDto): Promise<UserResponseDto> {
Expand Down

0 comments on commit ccfbfe0

Please sign in to comment.