@@ -6,18 +6,25 @@ import {
6
6
IFeatureTypeStore ,
7
7
} from '../types/stores/feature-type-store' ;
8
8
import NotFoundError from '../error/notfound-error' ;
9
+ import EventService from './event-service' ;
10
+ import { FEATURE_FAVORITED , FEATURE_TYPE_UPDATED , IUser } from '../types' ;
11
+ import { extractUsernameFromUser } from '../util' ;
9
12
10
13
export default class FeatureTypeService {
11
14
private featureTypeStore : IFeatureTypeStore ;
12
15
16
+ private eventService : EventService ;
17
+
13
18
private logger : Logger ;
14
19
15
20
constructor (
16
21
{ featureTypeStore } : Pick < IUnleashStores , 'featureTypeStore' > ,
17
22
{ getLogger } : Pick < IUnleashConfig , 'getLogger' > ,
23
+ eventService : EventService ,
18
24
) {
19
25
this . featureTypeStore = featureTypeStore ;
20
26
this . logger = getLogger ( 'services/feature-type-service.ts' ) ;
27
+ this . eventService = eventService ;
21
28
}
22
29
23
30
async getAll ( ) : Promise < IFeatureType [ ] > {
@@ -27,23 +34,33 @@ export default class FeatureTypeService {
27
34
async updateLifetime (
28
35
id : string ,
29
36
newLifetimeDays : number | null ,
37
+ user : IUser ,
30
38
) : Promise < IFeatureType > {
31
39
// because our OpenAPI library does type coercion, any `null` values you
32
40
// pass in get converted to `0`.
33
41
const translatedLifetime =
34
42
newLifetimeDays === 0 ? null : newLifetimeDays ;
35
43
44
+ const featureType = await this . featureTypeStore . get ( id ) ;
45
+
36
46
const result = await this . featureTypeStore . updateLifetime (
37
47
id ,
38
48
translatedLifetime ,
39
49
) ;
40
50
41
- if ( ! result ) {
51
+ if ( ! featureType || ! result ) {
42
52
throw new NotFoundError (
43
53
`The feature type you tried to update ("${ id } ") does not exist.` ,
44
54
) ;
45
55
}
46
56
57
+ await this . eventService . storeEvent ( {
58
+ type : FEATURE_TYPE_UPDATED ,
59
+ createdBy : extractUsernameFromUser ( user ) ,
60
+ data : { ...featureType , lifetimeDays : translatedLifetime } ,
61
+ preData : featureType ,
62
+ } ) ;
63
+
47
64
return result ;
48
65
}
49
66
}
0 commit comments