99 AddressUpdateRequest ,
1010} from 'src/model/address.model' ;
1111import { AddressValidation } from './address.validation' ;
12- import { Address , User } from '@prisma/client' ;
12+ import { Address , Contact , User } from '@prisma/client' ;
1313
1414@Injectable ( )
1515export class AddressService {
@@ -31,14 +31,33 @@ export class AddressService {
3131 } ;
3232 }
3333
34+ private async checkContactIsExistAndBelongsToUser (
35+ contactId : number ,
36+ userId : number ,
37+ ) : Promise < Contact > {
38+ return await this . prismaService . contact . findFirst ( {
39+ where : {
40+ id : contactId ,
41+ userId : userId ,
42+ } ,
43+ } ) ;
44+ }
45+
46+ private async checkAddressIsExistAndBelongsToContact (
47+ addressId : number ,
48+ contactId : number ,
49+ ) : Promise < Address > {
50+ return await this . prismaService . address . findFirst ( {
51+ where : {
52+ id : addressId ,
53+ contactId : contactId ,
54+ } ,
55+ } ) ;
56+ }
57+
3458 async getAddress ( user : User , contactId : number ) : Promise < AddressResponse [ ] > {
3559 const checkContactIsExistAndBelongsToUser =
36- await this . prismaService . contact . findFirst ( {
37- where : {
38- id : contactId ,
39- userId : user . id ,
40- } ,
41- } ) ;
60+ await this . checkContactIsExistAndBelongsToUser ( contactId , user . id ) ;
4261
4362 if ( ! checkContactIsExistAndBelongsToUser ) {
4463 throw new HttpException ( 'Contact not found' , 404 ) ;
@@ -59,12 +78,7 @@ export class AddressService {
5978 address : AddressRequest ,
6079 ) : Promise < AddressResponse > {
6180 const checkContactIsExistAndBelongsToUser =
62- await this . prismaService . contact . findFirst ( {
63- where : {
64- id : contactId ,
65- userId : user . id ,
66- } ,
67- } ) ;
81+ await this . checkContactIsExistAndBelongsToUser ( contactId , user . id ) ;
6882
6983 if ( ! checkContactIsExistAndBelongsToUser ) {
7084 throw new HttpException ( 'Contact not found' , 404 ) ;
@@ -91,12 +105,7 @@ export class AddressService {
91105 addressId : number ,
92106 ) : Promise < AddressResponse > {
93107 const checkContactIsExistAndBelongsToUser =
94- await this . prismaService . contact . findFirst ( {
95- where : {
96- id : contactId ,
97- userId : user . id ,
98- } ,
99- } ) ;
108+ await this . checkContactIsExistAndBelongsToUser ( contactId , user . id ) ;
100109
101110 if ( ! checkContactIsExistAndBelongsToUser ) {
102111 throw new HttpException ( 'Contact not found' , 404 ) ;
@@ -122,12 +131,7 @@ export class AddressService {
122131 address : AddressUpdateRequest ,
123132 ) : Promise < AddressResponse > {
124133 const checkContactIsExistAndBelongsToUser =
125- await this . prismaService . contact . findFirst ( {
126- where : {
127- id : contactId ,
128- userId : user . id ,
129- } ,
130- } ) ;
134+ await this . checkContactIsExistAndBelongsToUser ( contactId , user . id ) ;
131135
132136 if ( ! checkContactIsExistAndBelongsToUser ) {
133137 throw new HttpException ( 'Contact not found' , 404 ) ;
@@ -137,12 +141,7 @@ export class AddressService {
137141 this . validationService . validate ( AddressValidation . UPDATE , address ) ;
138142
139143 const checkAddressIsExistAndBelongsToContact =
140- await this . prismaService . address . findFirst ( {
141- where : {
142- id : addressId ,
143- contactId : contactId ,
144- } ,
145- } ) ;
144+ await this . checkAddressIsExistAndBelongsToContact ( addressId , contactId ) ;
146145
147146 if ( ! checkAddressIsExistAndBelongsToContact ) {
148147 throw new HttpException ( 'Address not found' , 404 ) ;
@@ -166,24 +165,14 @@ export class AddressService {
166165 addressId : number ,
167166 ) : Promise < any > {
168167 const checkContactIsExistAndBelongsToUser =
169- await this . prismaService . contact . findFirst ( {
170- where : {
171- id : contactId ,
172- userId : user . id ,
173- } ,
174- } ) ;
168+ await this . checkContactIsExistAndBelongsToUser ( contactId , user . id ) ;
175169
176170 if ( ! checkContactIsExistAndBelongsToUser ) {
177171 throw new HttpException ( 'Contact not found' , 404 ) ;
178172 }
179173
180174 const checkAddressIsExistAndBelongsToContact =
181- await this . prismaService . address . findFirst ( {
182- where : {
183- id : addressId ,
184- contactId : contactId ,
185- } ,
186- } ) ;
175+ await this . checkAddressIsExistAndBelongsToContact ( addressId , contactId ) ;
187176
188177 if ( ! checkAddressIsExistAndBelongsToContact ) {
189178 throw new HttpException ( 'Address not found' , 404 ) ;
0 commit comments