11package com .featureprobe .api .service ;
22
33
4+ import com .featureprobe .api .base .enums .ResourceType ;
5+ import com .featureprobe .api .base .exception .ResourceNotFoundException ;
46import com .featureprobe .api .dto .EventCreateRequest ;
7+ import com .featureprobe .api .entity .Environment ;
58import com .featureprobe .api .entity .Event ;
69import com .featureprobe .api .model .VariationAccessCounter ;
10+ import com .featureprobe .api .repository .EnvironmentRepository ;
711import com .featureprobe .api .repository .EventRepository ;
812import lombok .AllArgsConstructor ;
913import lombok .extern .slf4j .Slf4j ;
2226public class EventService {
2327
2428 private EventRepository eventRepository ;
29+ private EnvironmentRepository environmentRepository ;
2530
2631 public void create (String serverSdkKey , List <EventCreateRequest > requests ) {
32+ Environment environment = environmentRepository .findByServerSdkKey (serverSdkKey )
33+ .orElseThrow (() -> new ResourceNotFoundException (ResourceType .ENVIRONMENT , serverSdkKey ));
34+
2735 requests .forEach (request -> {
2836 if (request .getAccess () == null ) {
2937 return ;
@@ -33,7 +41,7 @@ public void create(String serverSdkKey, List<EventCreateRequest> requests) {
3341 .entrySet ()
3442 .stream ()
3543 .flatMap (entry -> createEventEntities (entry ).stream ())
36- .map (event -> wrapEvent (event , serverSdkKey , request ))
44+ .map (event -> wrapEvent (event , environment , request ))
3745 .collect (Collectors .toList ());
3846
3947 if (!events .isEmpty ()) {
@@ -57,16 +65,19 @@ private Event createEventEntity(String toggleKey, VariationAccessCounter accessC
5765 Event event = new Event ();
5866 event .setToggleKey (toggleKey );
5967 event .setCount (accessCounter .getCount ());
60- event .setVariation (accessCounter .getValue ());
68+ event .setValueIndex (accessCounter .getIndex ());
69+ event .setToggleVersion (accessCounter .getVersion ());
6170
6271 return event ;
6372 }
6473
65- private Event wrapEvent (Event event , String serverSdkKey , EventCreateRequest request ) {
74+ private Event wrapEvent (Event event , Environment environment , EventCreateRequest request ) {
6675 if (request .getAccess () == null ) {
6776 return event ;
6877 }
69- event .setSdkKey (serverSdkKey );
78+ event .setSdkKey (environment .getServerSdkKey ());
79+ event .setProjectKey (environment .getProject ().getKey ());
80+ event .setEnvironmentKey (environment .getKey ());
7081 event .setType ("access" );
7182 event .setStartDate (new Date (request .getAccess ().getStartTime ()));
7283 event .setEndDate (new Date (request .getAccess ().getEndTime ()));
0 commit comments