Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 1x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, type FindOptionsWhere } from 'typeorm';
import { Team, TeamAssignment, User } from '@amalia/core/models';
import { type Company } from '@amalia/tenants/companies/types';
import { makeSampleTeamAssignments } from '../../../assets/sampleData/teamAssignments';
import { sampleTeams } from '../../../assets/sampleData/teams';
import { sampleUsers } from '../../../assets/sampleData/users';
@Injectable()
export class SampleDataUserManagementService {
public constructor(
@InjectRepository(Team)
private readonly teamRepository: Repository<Team>,
@InjectRepository(TeamAssignment)
private readonly teamAssignmentRepository: Repository<TeamAssignment>,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
/**
* Loads sample data for a given company.
* @param company company to create
* @param emailPrefix
*/
public async load(company: Company, emailPrefix: string): Promise<{ users: User[]; teams: Team[] }> {
const users: User[] = await Promise.all(
sampleUsers.map(async (user) =>
this.userRepository.save({
...user,
email: `${emailPrefix}${user.email}`.toLowerCase(),
company,
}),
),
);
const teams: Team[] = await Promise.all(
sampleTeams.map(async (team) =>
this.teamRepository.save({
...team,
company,
}),
),
);
await Promise.all(
makeSampleTeamAssignments(teams, users, emailPrefix).map(async (teamAssignment) =>
this.teamAssignmentRepository.save({
...teamAssignment,
company,
}),
),
);
return { users, teams };
}
public async purge(company: Company): Promise<void> {
const companyConditions: FindOptionsWhere<User> = {
company: { id: company.id },
};
await this.teamAssignmentRepository.delete(companyConditions);
await this.teamRepository.delete(companyConditions);
await this.userRepository.delete(companyConditions);
}
}
|