1212use Symfony \Component \HttpFoundation \Response ;
1313use Symfony \Component \HttpKernel \Exception \HttpException ;
1414use Symfony \Contracts \Cache \CacheInterface ;
15+ use Symfony \Contracts \Cache \CacheInterface as ContractsCacheInterface ;
1516
1617/**
1718 * Class TokenService
@@ -22,10 +23,12 @@ class TokenService extends Service
2223 use NetworkControllerTrait;
2324
2425 private CacheInterface $ cache ;
26+ private ContractsCacheInterface $ tokensCache ;
2527
26- public function __construct (EntityManagerInterface $ entityManager , CacheInterface $ cache )
28+ public function __construct (EntityManagerInterface $ entityManager , CacheInterface $ cache, ContractsCacheInterface $ tokensCache )
2729 {
2830 $ this ->cache = $ cache ;
31+ $ this ->tokensCache = $ tokensCache ;
2932
3033 parent ::__construct ($ entityManager );
3134 }
@@ -85,7 +88,12 @@ public function getToken(RequestContextService $ctx, string $uuid): JsonResponse
8588 throw new HttpException (Response::HTTP_NOT_FOUND , 'Token not found ' );
8689 }
8790
88- return new JsonResponse ($ token ->__toArray ($ ctx ), Response::HTTP_OK );
91+ $ userAuth = [
92+ 'isAuthenticated ' => $ ctx ->isAuthenticated (),
93+ 'isAdmin ' => $ ctx ->isAdmin ()
94+ ];
95+
96+ return new JsonResponse ($ token ->__toArray ($ userAuth ), Response::HTTP_OK );
8997 }
9098
9199 /**
@@ -115,6 +123,12 @@ public function createToken(array $dataJson = [], bool $deprecated = false): Jso
115123
116124 $ this ->createOrUpdateToken ($ token , $ parsedJson , $ count );
117125 $ this ->em ->flush ();
126+ // Invalidate tokens cache pool after creation
127+ try {
128+ $ this ->tokensCache ->clear ();
129+ } catch (\Throwable $ e ) {
130+ // don't break the API if cache clear fails
131+ }
118132 } else { // Multiple
119133 $ tokens = $ tokenRepository ->findBy (['uuid ' => array_column ($ parsedJson , 'uuid ' )]);
120134
@@ -190,6 +204,13 @@ public function updateToken(string $contractAddress, array $dataJson = []): Json
190204 $ this ->em ->persist ($ token );
191205 $ this ->em ->flush ();
192206
207+ // Invalidate tokens cache pool after update
208+ try {
209+ $ this ->tokensCache ->clear ();
210+ } catch (\Throwable $ e ) {
211+ // ignore cache errors
212+ }
213+
193214 return new JsonResponse (
194215 ["status " => "success " , "message " => "Token updated successfully " ],
195216 Response::HTTP_ACCEPTED
@@ -210,6 +231,13 @@ public function deleteToken(string $contractAddress): JsonResponse
210231 $ this ->em ->remove ($ token );
211232 $ this ->em ->flush ();
212233
234+ // Invalidate tokens cache pool after delete
235+ try {
236+ $ this ->tokensCache ->clear ();
237+ } catch (\Throwable $ e ) {
238+ // ignore
239+ }
240+
213241 return new JsonResponse (
214242 ["status " => "success " , "message " => "Token deleted successfully " ],
215243 Response::HTTP_OK
@@ -230,7 +258,12 @@ public function showLatestUpdated(RequestContextService $ctx): JsonResponse
230258 /** @var Token $lastTokenUpdated */
231259 $ lastTokenUpdated = $ tokenRepository ->getLastTokenUpdated ()[0 ];
232260
233- return new JsonResponse ($ lastTokenUpdated ->__toArray ($ ctx ), Response::HTTP_OK );
261+ $ userAuth = [
262+ 'isAuthenticated ' => $ ctx ->isAuthenticated (),
263+ 'isAdmin ' => $ ctx ->isAdmin ()
264+ ];
265+
266+ return new JsonResponse ($ lastTokenUpdated ->__toArray ($ userAuth ), Response::HTTP_OK );
234267 }
235268
236269 /**
0 commit comments