11import path from 'node:path' ;
2- import { ToppingCategory } from './topping' ;
3- import { Burger } from './burger' ;
4- import { Topping } from './topping' ;
5- import { Order , OrderStatus } from './order' ;
62import { Container , CosmosClient , Database } from '@azure/cosmos' ;
73import { DefaultAzureCredential } from '@azure/identity' ;
4+ import dotenv from 'dotenv' ;
85import burgersData from '../data/burgers.json' ;
96import toppingsData from '../data/toppings.json' ;
10- import dotenv from 'dotenv' ;
7+ import { ToppingCategory , Topping } from './topping.js' ;
8+ import { Burger } from './burger.js' ;
9+ import { Order , OrderStatus } from './order.js' ;
1110
1211// Env file is located in the root of the repository
1312dotenv . config ( { path : path . join ( process . cwd ( ) , '../../.env' ) } ) ;
1413
1514// Helper to strip properties starting with underscore from an object
16- function stripUnderscoreProps < T extends object > ( obj : T ) : T {
17- if ( ! obj || typeof obj !== 'object' ) return obj ;
18- const result : { [ key : string ] : any } = { } ;
19- for ( const key of Object . keys ( obj ) ) {
15+ function stripUnderscoreProperties < T extends Record < string , unknown > > ( object : T ) : T {
16+ if ( ! object || typeof object !== 'object' ) return object ;
17+ const result : Record < string , any > = { } ;
18+ for ( const key of Object . keys ( object ) ) {
2019 if ( ! key . startsWith ( '_' ) ) {
21- result [ key ] = ( obj as any ) [ key ] ;
20+ result [ key ] = ( object as any ) [ key ] ;
2221 }
2322 }
23+
2424 return result as T ;
2525}
2626
@@ -33,10 +33,12 @@ function stripUserId<T extends Order | Order[] | undefined>(orderOrOrders: T): T
3333 return rest as Order ;
3434 } ) as T ;
3535 }
36+
3637 if ( orderOrOrders && typeof orderOrOrders === 'object' ) {
37- const { userId, ...rest } = orderOrOrders as Order ;
38+ const { userId, ...rest } = orderOrOrders ;
3839 return rest as T ;
3940 }
41+
4042 return orderOrOrders ;
4143}
4244
@@ -62,6 +64,7 @@ export class DbService {
6264 await DbService . instance . initializeCosmosDb ( ) ;
6365 DbService . instance . initializeLocalData ( ) ;
6466 }
67+
6568 return DbService . instance ;
6669 }
6770
@@ -180,25 +183,27 @@ export class DbService {
180183 query : 'SELECT * FROM c' ,
181184 } ;
182185 const { resources } = await this . burgersContainer ! . items . query ( querySpec ) . fetchAll ( ) ;
183- return ( resources as Burger [ ] ) . map ( stripUnderscoreProps ) ;
186+ return ( resources as Burger [ ] ) . map ( stripUnderscoreProperties ) ;
184187 } catch ( error ) {
185188 console . error ( 'Error fetching burgers from Cosmos DB:' , error ) ;
186189 return [ ...this . localBurgers ] ;
187190 }
188191 }
192+
189193 return [ ...this . localBurgers ] ;
190194 }
191195
192196 async getBurger ( id : string ) : Promise < Burger | undefined > {
193197 if ( this . isCosmosDbInitialized ) {
194198 try {
195199 const { resource } = await this . burgersContainer ! . item ( id , id ) . read ( ) ;
196- return resource ? stripUnderscoreProps ( resource as Burger ) : undefined ;
200+ return resource ? stripUnderscoreProperties ( resource as Burger ) : undefined ;
197201 } catch ( error ) {
198202 console . error ( `Error fetching burger ${ id } from Cosmos DB:` , error ) ;
199203 return this . localBurgers . find ( ( burger ) => burger . id === id ) ;
200204 }
201205 }
206+
202207 return this . localBurgers . find ( ( burger ) => burger . id === id ) ;
203208 }
204209
@@ -210,25 +215,27 @@ export class DbService {
210215 query : 'SELECT * FROM c' ,
211216 } ;
212217 const { resources } = await this . toppingsContainer ! . items . query ( querySpec ) . fetchAll ( ) ;
213- return ( resources as Topping [ ] ) . map ( stripUnderscoreProps ) ;
218+ return ( resources as Topping [ ] ) . map ( stripUnderscoreProperties ) ;
214219 } catch ( error ) {
215220 console . error ( 'Error fetching toppings from Cosmos DB:' , error ) ;
216221 return [ ...this . localToppings ] ;
217222 }
218223 }
224+
219225 return [ ...this . localToppings ] ;
220226 }
221227
222228 async getTopping ( id : string ) : Promise < Topping | undefined > {
223229 if ( this . isCosmosDbInitialized ) {
224230 try {
225231 const { resource } = await this . toppingsContainer ! . item ( id , id ) . read ( ) ;
226- return resource ? stripUnderscoreProps ( resource as Topping ) : undefined ;
232+ return resource ? stripUnderscoreProperties ( resource as Topping ) : undefined ;
227233 } catch ( error ) {
228234 console . error ( `Error fetching topping ${ id } from Cosmos DB:` , error ) ;
229235 return this . localToppings . find ( ( topping ) => topping . id === id ) ;
230236 }
231237 }
238+
232239 return this . localToppings . find ( ( topping ) => topping . id === id ) ;
233240 }
234241
@@ -257,14 +264,16 @@ export class DbService {
257264 query : 'SELECT * FROM c' ,
258265 } ;
259266 }
267+
260268 const { resources } = await this . ordersContainer ! . items . query ( querySpec ) . fetchAll ( ) ;
261- return stripUserId ( ( resources as Order [ ] ) . map ( stripUnderscoreProps ) ) ;
269+ return stripUserId ( ( resources as Order [ ] ) . map ( stripUnderscoreProperties ) ) ;
262270 } catch ( error ) {
263271 console . error ( 'Error fetching orders from Cosmos DB:' , error ) ;
264272 const orders = userId ? this . localOrders . filter ( ( order ) => order . userId === userId ) : this . localOrders ;
265273 return stripUserId ( orders ) ;
266274 }
267275 }
276+
268277 const orders = userId ? this . localOrders . filter ( ( order ) => order . userId === userId ) : this . localOrders ;
269278 return stripUserId ( orders ) ;
270279 }
@@ -275,10 +284,11 @@ export class DbService {
275284 const { resource } = await this . ordersContainer ! . item ( id , id ) . read ( ) ;
276285 if ( ! resource ) return undefined ;
277286
278- const order = stripUnderscoreProps ( resource as Order ) ;
287+ const order = stripUnderscoreProperties ( resource as Order ) ;
279288 if ( userId && order . userId !== userId ) {
280289 return undefined ;
281290 }
291+
282292 return stripUserId ( order ) ;
283293 } catch ( error ) {
284294 console . error ( `Error fetching order ${ id } from Cosmos DB:` , error ) ;
@@ -287,28 +297,32 @@ export class DbService {
287297 if ( userId && order . userId !== userId ) {
288298 return undefined ;
289299 }
300+
290301 return stripUserId ( order ) ;
291302 }
292303 }
304+
293305 const order = this . localOrders . find ( ( order ) => order . id === id ) ;
294306 if ( ! order ) return undefined ;
295307 if ( userId && order . userId !== userId ) {
296308 return undefined ;
297309 }
310+
298311 return stripUserId ( order ) ;
299312 }
300313
301314 async createOrder ( order : Order ) : Promise < Order > {
302315 if ( this . isCosmosDbInitialized ) {
303316 try {
304317 const { resource } = await this . ordersContainer ! . items . create ( order ) ;
305- return stripUserId ( stripUnderscoreProps ( resource as Order ) ) ;
318+ return stripUserId ( stripUnderscoreProperties ( resource as Order ) ) ;
306319 } catch ( error ) {
307320 console . error ( 'Error creating order in Cosmos DB:' , error ) ;
308321 this . localOrders . push ( order ) ;
309322 return stripUserId ( order ) ;
310323 }
311324 }
325+
312326 this . localOrders . push ( order ) ;
313327 return stripUserId ( order ) ;
314328 }
@@ -325,7 +339,7 @@ export class DbService {
325339
326340 const updatedOrder = { ...existingOrder , status } ;
327341 const { resource } = await this . ordersContainer ! . item ( id , id ) . replace ( updatedOrder ) ;
328- return stripUserId ( stripUnderscoreProps ( resource as Order ) ) ;
342+ return stripUserId ( stripUnderscoreProperties ( resource as Order ) ) ;
329343 } catch ( error ) {
330344 console . error ( `Error updating order ${ id } in Cosmos DB:` , error ) ;
331345 const orderIndex = this . localOrders . findIndex ( ( order ) => order . id === id ) ;
@@ -340,6 +354,7 @@ export class DbService {
340354 return stripUserId ( this . localOrders [ orderIndex ] ) ;
341355 }
342356 }
357+
343358 const orderIndex = this . localOrders . findIndex ( ( order ) => order . id === id ) ;
344359 if ( orderIndex === - 1 ) return undefined ;
345360
@@ -378,6 +393,7 @@ export class DbService {
378393 return true ;
379394 }
380395 }
396+
381397 const orderIndex = this . localOrders . findIndex ( ( order ) => order . id === id ) ;
382398 if ( orderIndex === - 1 ) return false ;
383399
@@ -398,7 +414,7 @@ export class DbService {
398414
399415 const updatedOrder = { ...existingOrder , ...updates } ;
400416 const { resource } = await this . ordersContainer ! . item ( id , id ) . replace ( updatedOrder ) ;
401- return stripUserId ( stripUnderscoreProps ( resource as Order ) ) ;
417+ return stripUserId ( stripUnderscoreProperties ( resource as Order ) ) ;
402418 } catch ( error ) {
403419 console . error ( `Error updating order ${ id } in Cosmos DB:` , error ) ;
404420 const orderIndex = this . localOrders . findIndex ( ( order ) => order . id === id ) ;
@@ -408,6 +424,7 @@ export class DbService {
408424 return stripUserId ( this . localOrders [ orderIndex ] ) ;
409425 }
410426 }
427+
411428 const orderIndex = this . localOrders . findIndex ( ( order ) => order . id === id ) ;
412429 if ( orderIndex === - 1 ) return undefined ;
413430
@@ -456,7 +473,7 @@ export class DbService {
456473
457474 try {
458475 const { resource } = await this . usersContainer . item ( id , id ) . read ( ) ;
459- return ! ! resource ;
476+ return Boolean ( resource ) ;
460477 } catch ( error ) {
461478 console . error ( 'Error checking user existence:' , error ) ;
462479 return false ;
0 commit comments