chore: create legal and real user data info
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { Controller, Get, Query } from "@nestjs/common";
|
||||
import { Controller, Get, Param, Query } from "@nestjs/common";
|
||||
import { ApiOperation } from "@nestjs/swagger";
|
||||
|
||||
import { SearchCitiesDto } from "./DTO/cities-search-query.dto";
|
||||
import { SearchProvincesDto } from "./DTO/provinces-search-query.dto";
|
||||
import { AddressService } from "./providers/address.service";
|
||||
import { ParamNumberIdDto } from "../../common/DTO/param.dto";
|
||||
|
||||
@Controller("address")
|
||||
export class AddressController {
|
||||
@@ -20,4 +21,16 @@ export class AddressController {
|
||||
getProvincesList(@Query() queryDto: SearchProvincesDto) {
|
||||
return this.addressService.getAllProvinces(queryDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Get cities of provinces" })
|
||||
@Get("provinces/:id/cities")
|
||||
getCitiesByProvinces(@Query() queryDto: SearchProvincesDto, @Param() paramDto: ParamNumberIdDto) {
|
||||
return this.addressService.getCitiesByProvince(queryDto, +paramDto.id);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Get province of city" })
|
||||
@Get("cities/:id/province")
|
||||
getProvinceByCity(@Param() paramDto: ParamNumberIdDto) {
|
||||
return this.addressService.getProvinceByCity(+paramDto.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,18 @@ import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
|
||||
import { AddressController } from "./address.controller";
|
||||
import { Address } from "./entities/address.entity";
|
||||
import { City } from "./entities/city.entity";
|
||||
import { Province } from "./entities/province.entity";
|
||||
import { AddressService } from "./providers/address.service";
|
||||
import { AddressRepository } from "./repositories/address.repository";
|
||||
import { CityRepository } from "./repositories/city.repository";
|
||||
import { ProvinceRepository } from "./repositories/province.repository";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([City, Province])],
|
||||
providers: [AddressService, CityRepository, ProvinceRepository],
|
||||
imports: [TypeOrmModule.forFeature([City, Province, Address])],
|
||||
providers: [AddressService, CityRepository, ProvinceRepository, AddressRepository],
|
||||
controllers: [AddressController],
|
||||
exports: [TypeOrmModule, CityRepository, ProvinceRepository, AddressService],
|
||||
})
|
||||
export class AddressModule {}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Column, Entity, ManyToOne, OneToOne } from "typeorm";
|
||||
|
||||
import { City } from "./city.entity";
|
||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
|
||||
@Entity()
|
||||
export class Address extends BaseEntity {
|
||||
@Column({ type: "varchar", length: 255, nullable: true })
|
||||
address: string;
|
||||
|
||||
@Column({ type: "varchar", length: 10, nullable: true })
|
||||
postalCode: string;
|
||||
|
||||
@ManyToOne(() => City, (city) => city.addresses, { nullable: false })
|
||||
city: City;
|
||||
|
||||
@OneToOne(() => User, (user) => user.address, { onDelete: "CASCADE" })
|
||||
user: User;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
import { Address } from "./address.entity";
|
||||
import { Province } from "./province.entity";
|
||||
|
||||
@Entity()
|
||||
@@ -10,6 +11,9 @@ export class City {
|
||||
@Column({ type: "varchar", length: 255 })
|
||||
name: string;
|
||||
|
||||
@OneToMany(() => Address, (address) => address.city)
|
||||
addresses: Address[];
|
||||
|
||||
@ManyToOne(() => Province, (province) => province.cities, { nullable: false })
|
||||
province: Province;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
|
||||
import { CityMessage, ProvinceMessage } from "../../../common/enums/message.enum";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { SearchCitiesDto } from "../DTO/cities-search-query.dto";
|
||||
import { CityRepository } from "../repositories/city.repository";
|
||||
@@ -51,4 +52,59 @@ export class AddressService {
|
||||
paginate: true,
|
||||
};
|
||||
}
|
||||
|
||||
async getProvince(id: number) {
|
||||
const province = await this.provinceRepository.findOneBy({
|
||||
id,
|
||||
});
|
||||
if (!province) throw new BadRequestException(CityMessage.NOT_FOUND);
|
||||
|
||||
return { province };
|
||||
}
|
||||
|
||||
async getCity(id: number) {
|
||||
const city = await this.cityRepository.findOneBy({
|
||||
id,
|
||||
});
|
||||
if (!city) throw new BadRequestException(CityMessage.NOT_FOUND);
|
||||
|
||||
return { city };
|
||||
}
|
||||
|
||||
async getCitiesByProvince(queryDto: SearchCitiesDto, id: number) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
const province = await this.provinceRepository.findOneBy({
|
||||
id,
|
||||
});
|
||||
if (!province) throw new BadRequestException(CityMessage.NOT_FOUND);
|
||||
|
||||
const queryBuilder = this.cityRepository.createQueryBuilder("city").where("city.provinceId = :provinceId", { provinceId: province.id });
|
||||
if (queryDto.q) {
|
||||
queryBuilder.andWhere("city.name ILIKE :q", { q: `%${queryDto.q}%` });
|
||||
}
|
||||
|
||||
queryBuilder.skip(skip).take(limit);
|
||||
|
||||
const [cities, count] = await queryBuilder.getManyAndCount();
|
||||
|
||||
return {
|
||||
cities,
|
||||
count,
|
||||
pagination: true,
|
||||
};
|
||||
}
|
||||
|
||||
async getProvinceByCity(id: number) {
|
||||
const province = await this.provinceRepository.findOne({
|
||||
where: {
|
||||
cities: {
|
||||
id,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!province) throw new BadRequestException(ProvinceMessage.NOT_FOUND);
|
||||
|
||||
return { province };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
import { Address } from "../entities/address.entity";
|
||||
|
||||
@Injectable()
|
||||
export class AddressRepository extends Repository<Address> {
|
||||
constructor(@InjectRepository(Address) addressRepository: Repository<Address>) {
|
||||
super(addressRepository.target, addressRepository.manager, addressRepository.queryRunner);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user