1
- import { Injectable } from '@nestjs/common' ;
1
+ import { Injectable , Logger } from '@nestjs/common' ;
2
2
import { InjectModel } from '@nestjs/mongoose' ;
3
3
import { Movie , MovieDocument } from './schemas/movie.schema' ;
4
4
import { Model } from 'mongoose' ;
5
- import { BaseService } from 'src/common/base/base.service' ;
6
5
import { getRandomInt } from 'src/common/utils/get-random-int.util' ;
7
6
import { GetPossibleValueDto } from './dto/get-possible-values.dto' ;
8
7
import { PossibleValueDto } from './dto/response/possible-value.response.dto' ;
@@ -17,13 +16,16 @@ import { MOVIE_INDEX } from './constants/movie-index';
17
16
import { SearchDto } from 'src/common/dto/query/search.dto' ;
18
17
import { MovieDocsResponseDtoV1 } from './dto/v1/movie-docs.response.dto' ;
19
18
import { MovieDtoV1 } from './dto/v1/movie.dto' ;
19
+ import { ConfigService } from '@nestjs/config' ;
20
20
21
21
@Injectable ( )
22
22
export class MovieService {
23
+ private readonly logger = new Logger ( MovieService . name ) ;
23
24
constructor (
24
25
@InjectModel ( Movie . name ) private readonly movieModel : Model < MovieDocument > ,
25
26
@InjectModel ( MovieAward . name ) private readonly movieAwardModel : Model < MovieAwardDocument > ,
26
27
private readonly meiliService : MeiliService ,
28
+ private readonly configService : ConfigService ,
27
29
) { }
28
30
29
31
async findMany ( query : IQuery ) : Promise < MovieDocsResponseDtoV1 > {
@@ -52,8 +54,12 @@ export class MovieService {
52
54
53
55
async findOne ( id : number | string ) : Promise < MovieDtoV1 > {
54
56
const found = await this . movieModel . findOne ( { id } ) ;
55
- // @ts -ignore
56
- if ( found ) return found . toJSON ( ) ;
57
+ if ( found ) {
58
+ // @ts -ignore
59
+ return found . toJSON ( ) ;
60
+ } else {
61
+ await this . addMovie ( id ) ;
62
+ }
57
63
return found ;
58
64
}
59
65
@@ -116,4 +122,30 @@ export class MovieService {
116
122
pages : Math . ceil ( total / query . limit ) ,
117
123
} ;
118
124
}
125
+
126
+ async addMovie ( id : number | string ) : Promise < void > {
127
+ this . logger . log ( `Add movie with id: ${ id } ` ) ;
128
+ const baseUrl = this . configService . get ( 'UPDATE_API_BASE_URL' ) ;
129
+ const resp = await fetch ( `${ baseUrl } /movie` , {
130
+ method : 'PUT' ,
131
+ headers : { 'Content-Type' : 'application/json' } ,
132
+ body : JSON . stringify ( {
133
+ updateData : [
134
+ 'base' ,
135
+ 'premiere' ,
136
+ 'facts' ,
137
+ 'fees' ,
138
+ 'budget' ,
139
+ 'videos' ,
140
+ 'similarMovies' ,
141
+ 'images' ,
142
+ 'persons' ,
143
+ 'allDataPersons' ,
144
+ 'sequelsAndPrequels' ,
145
+ 'reviews' ,
146
+ ] ,
147
+ ids : [ id ] ,
148
+ } ) ,
149
+ } ) ;
150
+ }
119
151
}
0 commit comments