Skip to content

Commit 3f00d32

Browse files
committed
feat: create get all companies by an user function
1 parent 35fa868 commit 3f00d32

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/company/company.service.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class CompanyService {
1616
@InjectRepository(User)
1717
private userRepository: Repository<User>,
1818
) {}
19-
19+
2020
findAll(paginationDTO: PaginationDTO): Promise<Company[]> {
2121
return this.companyRepository.find({
2222
relations: ['user'],
@@ -25,12 +25,46 @@ export class CompanyService {
2525
});
2626
}
2727

28+
findAllByUser(userId: number, paginationDTO: PaginationDTO): Promise<Company[]> {
29+
return this.companyRepository.find({
30+
where: { user: { id: userId } },
31+
relations: ['user'],
32+
skip: paginationDTO.skip,
33+
take: paginationDTO.limit ?? DEFAULT_PAGE_SIZE,
34+
select: {
35+
id: true,
36+
name: true,
37+
phoneNumber: true,
38+
cnpj: true,
39+
user: {
40+
id: true,
41+
username: true,
42+
email: true,
43+
},
44+
},
45+
});
46+
}
47+
2848
findOne(id: number): Promise<Company> {
29-
return this.companyRepository.findOne({ where: { id }, relations: ['user'] });
49+
return this.companyRepository.findOne({
50+
where: { id },
51+
relations: ['user'],
52+
select: {
53+
id: true,
54+
name: true,
55+
user: {
56+
id: true,
57+
username: true,
58+
}
59+
}});
3060
}
3161

3262
async create(userId: number, body: CreateCompanyDto): Promise<Company> {
33-
const user = await this.userRepository.findOne({where: {id: userId}});
63+
const user = await this.userRepository.findOne({
64+
where:
65+
{id: userId},
66+
select: ['id', 'username', 'email', 'role']
67+
});
3468
if (!user) {
3569
throw new NotFoundException(`User with ID ${userId} not found`);
3670
}

0 commit comments

Comments
 (0)