@@ -6,10 +6,10 @@ import fs from 'fs';
66import path from 'path' ;
77import dotenv from 'dotenv' ;
88
9- import productsJSON from './response/products.json' assert { type : 'json ' } ;
10- import categoriesJSON from './response/categories.json' assert { type : 'json ' } ;
11- import couponListJSON from './response/couponList.json' assert { type : 'json ' } ;
12- import usersJSON from './response/users.json' assert { type : 'json ' } ;
9+ import productsJSON from './response/products.json' with { type : 'json' } ;
10+ import categoriesJSON from './response/categories.json' with { type : 'json' } ;
11+ import couponListJSON from './response/couponList.json' with { type : 'json' } ;
12+ import usersJSON from './response/users.json' with { type : 'json' } ;
1313
1414dotenv . config ( ) ;
1515
@@ -23,7 +23,7 @@ app.use(express.json());
2323app . use ( cors ( ) ) ;
2424
2525function wait ( amount = 0 ) {
26- return new Promise ( ( resolve ) => setTimeout ( resolve , amount ) ) ;
26+ return new Promise ( resolve => setTimeout ( resolve , amount ) ) ;
2727}
2828
2929function getRandomArbitrary ( min , max ) {
@@ -40,42 +40,56 @@ function getProductsByCategoryId(products, categoryId) {
4040 const numberCategoryId = Number ( categoryId ) ;
4141
4242 return numberCategoryId > 0 && numberCategoryId < 6
43- ? products . filter ( ( item ) => item . category . id === numberCategoryId )
43+ ? products . filter ( item => item . category . id === numberCategoryId )
4444 : products ;
4545}
4646
4747function getProductsByPrice ( products , price ) {
48- return price ? products . filter ( ( item ) => item . price === Number ( price ) ) : products ;
48+ return price
49+ ? products . filter ( item => item . price === Number ( price ) )
50+ : products ;
4951}
5052
5153function getProductsByPriceRange ( { products, minPrice, maxPrice } ) {
5254 const minValue = minPrice ? Number ( minPrice ) : Number . MIN_SAFE_INTEGER ;
5355 const maxValue = maxPrice ? Number ( maxPrice ) : Number . MAX_SAFE_INTEGER ;
5456
55- return products . filter ( ( item ) => item . price >= minValue && item . price <= maxValue ) ;
57+ return products . filter (
58+ item => item . price >= minValue && item . price <= maxValue ,
59+ ) ;
5660}
5761
5862function getProductsByTitle ( products , title ) {
59- return title ? products . filter ( ( item ) => item . title . includes ( title ) ) : products ;
63+ return title ? products . filter ( item => item . title . includes ( title ) ) : products ;
6064}
6165
6266function getFilteredProductsByQuery ( products , query ) {
63- const productsFilteredByCategoryId = getProductsByCategoryId ( products , query . categoryId ) ;
67+ const productsFilteredByCategoryId = getProductsByCategoryId (
68+ products ,
69+ query . categoryId ,
70+ ) ;
6471 const productsFilteredByPriceRange = getProductsByPriceRange ( {
6572 products : productsFilteredByCategoryId ,
6673 minPrice : query . minPrice ,
6774 maxPrice : query . maxPrice ,
6875 } ) ;
69- const productsFilteredByPrice = getProductsByPrice ( productsFilteredByPriceRange , query . price ) ;
70- const productsFilteredByTitle = getProductsByTitle ( productsFilteredByPrice , query . title ) ;
76+ const productsFilteredByPrice = getProductsByPrice (
77+ productsFilteredByPriceRange ,
78+ query . price ,
79+ ) ;
80+ const productsFilteredByTitle = getProductsByTitle (
81+ productsFilteredByPrice ,
82+ query . title ,
83+ ) ;
7184
7285 return productsFilteredByTitle ;
7386}
7487
7588function getSliceIndex ( products , query ) {
7689 const offset = query . offset ? Number ( query . offset ) : 0 ;
7790 const limit = query . limit ? Number ( query . limit ) : products . length ;
78- const endIndex = offset + limit > products . length ? products . length : offset + limit ;
91+ const endIndex =
92+ offset + limit > products . length ? products . length : offset + limit ;
7993
8094 return {
8195 startIndex : offset ,
@@ -89,7 +103,11 @@ const IV_LENGTH = 16; // For AES, this is always 16
89103
90104function encrypt ( text ) {
91105 let iv = crypto . randomBytes ( IV_LENGTH ) ;
92- let cipher = crypto . createCipheriv ( 'aes-256-cbc' , Buffer . from ( SECURITY_KEY ) , iv ) ;
106+ let cipher = crypto . createCipheriv (
107+ 'aes-256-cbc' ,
108+ Buffer . from ( SECURITY_KEY ) ,
109+ iv ,
110+ ) ;
93111 let encrypted = cipher . update ( text ) ;
94112
95113 encrypted = Buffer . concat ( [ encrypted , cipher . final ( ) ] ) ;
@@ -101,7 +119,11 @@ function decrypt(text) {
101119 let textParts = text . split ( ':' ) ;
102120 let iv = Buffer . from ( textParts . shift ( ) , 'hex' ) ;
103121 let encryptedText = Buffer . from ( textParts . join ( ':' ) , 'hex' ) ;
104- let decipher = crypto . createDecipheriv ( 'aes-256-cbc' , Buffer . from ( SECURITY_KEY ) , iv ) ;
122+ let decipher = crypto . createDecipheriv (
123+ 'aes-256-cbc' ,
124+ Buffer . from ( SECURITY_KEY ) ,
125+ iv ,
126+ ) ;
105127 let decrypted = decipher . update ( encryptedText ) ;
106128
107129 decrypted = Buffer . concat ( [ decrypted , decipher . final ( ) ] ) ;
@@ -126,10 +148,14 @@ app.get('/user', (req, res) => {
126148
127149app . post ( '/login' , ( req , res ) => {
128150 const { email, password } = req . body ;
129- const result = usersJSON . users . find ( ( user ) => user . email === email && user . password === password ) ;
151+ const result = usersJSON . users . find (
152+ user => user . email === email && user . password === password ,
153+ ) ;
130154
131155 if ( ! result ) {
132- return res . status ( 401 ) . send ( { errorCode : 401000 , message : '정보가 일치하지 않습니다.' } ) ;
156+ return res
157+ . status ( 401 )
158+ . send ( { errorCode : 401000 , message : '정보가 일치하지 않습니다.' } ) ;
133159 }
134160
135161 const cookieConfig = {
@@ -148,27 +174,35 @@ app.post('/users', (req, res) => {
148174 const { users } = usersJSON ;
149175
150176 if ( ! password . length ) {
151- return res
152- . status ( 400 )
153- . send ( { field : 'password' , errorCode : 400001 , message : '패스워드를 입력해주세요!' } ) ;
177+ return res . status ( 400 ) . send ( {
178+ field : 'password' ,
179+ errorCode : 400001 ,
180+ message : '패스워드를 입력해주세요!' ,
181+ } ) ;
154182 }
155183
156184 if ( ! name . length ) {
157- return res
158- . status ( 400 )
159- . send ( { field : 'name' , errorCode : 400002 , message : '이메일을 입력해주세요!' } ) ;
185+ return res . status ( 400 ) . send ( {
186+ field : 'name' ,
187+ errorCode : 400002 ,
188+ message : '이메일을 입력해주세요!' ,
189+ } ) ;
160190 }
161191
162192 if ( ! email . length || ! EMAIL_PATTERN . test ( email ) ) {
163- return res
164- . status ( 400 )
165- . send ( { field : 'email' , errorCode : 400003 , message : '잘못된 이메일 양식입니다!' } ) ;
193+ return res . status ( 400 ) . send ( {
194+ field : 'email' ,
195+ errorCode : 400003 ,
196+ message : '잘못된 이메일 양식입니다!' ,
197+ } ) ;
166198 }
167199
168- if ( users . find ( ( user ) => user . email === email ) ) {
169- return res
170- . status ( 400 )
171- . send ( { field : 'email' , errorCode : 400004 , message : '이미 존재하는 메일입니다!' } ) ;
200+ if ( users . find ( user => user . email === email ) ) {
201+ return res . status ( 400 ) . send ( {
202+ field : 'email' ,
203+ errorCode : 400004 ,
204+ message : '이미 존재하는 메일입니다!' ,
205+ } ) ;
172206 }
173207
174208 const newUser = { id : users . length + 1 , name, email, password } ;
@@ -238,7 +272,7 @@ app.post('/log', (req, res) => {
238272 fs . appendFileSync (
239273 './server/paymentResult.log' ,
240274 `LOG[${ level } ]: userId: ${ userId } | date: ${ new Date ( ) } | message: ${ message } \n` ,
241- ( error ) => {
275+ error => {
242276 throw error ;
243277 } ,
244278 ) ;
0 commit comments