@@ -2,6 +2,13 @@ import * as functions from 'firebase-functions';
22import * as admin from 'firebase-admin' ;
33import * as express from 'express' ;
44import * as bodyParser from 'body-parser' ;
5+ import * as cors from 'cors' ;
6+
7+ interface Promotion {
8+ code : string ;
9+ dateExpiration : Date ;
10+ description : string ;
11+ }
512
613admin . initializeApp ( functions . config ( ) . firebase ) ;
714
@@ -10,6 +17,8 @@ const app = express();
1017const main = express ( ) ;
1118
1219//add the path to receive request and set json as bodyParser to process the body
20+ app . use ( cors ( { origin : true } ) ) ;
21+ main . use ( cors ( { origin : true } ) ) ;
1322main . use ( '/v1' , app ) ;
1423main . use ( bodyParser . json ( ) ) ;
1524main . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
@@ -22,38 +31,24 @@ const promoCollection = 'promotions';
2231//define google cloud function name
2332export const webServices = functions . region ( 'europe-west1' ) . https . onRequest ( main ) ;
2433
25-
2634app . get ( '/promotions/:promoId' , ( req , res ) => {
27-
2835 const promoId = req . params . promoId ;
2936 db . collection ( promoCollection ) . doc ( promoId ) . get ( )
3037 . then ( promo => {
3138 if ( ! promo . exists ) {
32- res . status ( 400 ) . json ( {
33- status : 'error' ,
34- message : 'Promotion introuvable.'
35- } ) ;
39+ res . status ( 400 ) . send ( 'Promotion introuvable.' ) ;
3640 } else {
3741 res . status ( 200 ) . json ( {
38- status : "success" ,
39- data : {
40- code : promo . data ( ) ?. code ,
41- dateExpiration : promo . data ( ) ?. dateExpiration . toDate ( ) ,
42- description : promo . data ( ) ?. description
43- }
42+ code : promo . data ( ) ?. code ,
43+ dateExpiration : promo . data ( ) ?. dateExpiration . toDate ( ) ,
44+ description : promo . data ( ) ?. description
4445 } ) ;
4546 }
4647 } )
4748 . catch ( error => res . status ( 500 ) . send ( error ) ) ;
4849} ) ;
4950
50- interface Promotion {
51- code : string ;
52- dateExpiration : Date ;
53- description : string ;
54- }
55-
56- app . post ( '/promotions' , ( req , res ) => {
51+ app . post ( '/promotions' , ( req , res ) => {
5752 try {
5853 const body = req . body ;
5954 const dateExpiration = new Date ( body . dateExpiration ) ;
@@ -92,18 +87,12 @@ app.get('/users/:userId', (req, res) => {
9287 db . collection ( userCollection ) . doc ( userId ) . get ( )
9388 . then ( user => {
9489 if ( ! user . exists ) {
95- res . status ( 400 ) . json ( {
96- status : 'error' ,
97- message : 'Utilisateur introuvable.'
98- } ) ;
90+ res . status ( 400 ) . send ( 'Utilisateur introuvable.' ) ;
9991 }
10092 res . status ( 200 ) . json ( {
101- status : "success" ,
102- data : {
103- uid : user . id ,
104- name : user . data ( ) ?. name ,
105- firstname : user . data ( ) ?. firstname
106- }
93+ uid : user . id ,
94+ name : user . data ( ) ?. name ,
95+ firstname : user . data ( ) ?. firstname
10796 } ) ;
10897 } ) . catch ( error => res . status ( 500 ) . send ( error ) ) ;
10998} ) ;
@@ -113,14 +102,11 @@ app.get('/users/:userId/promotions', (req, res) => {
113102 db . collection ( userCollection ) . doc ( userId ) . get ( )
114103 . then ( user => {
115104 if ( ! user . exists ) {
116- res . status ( 400 ) . json ( {
117- status : 'error' ,
118- message : 'Utilisateur introuvable.'
119- } ) ;
105+ res . status ( 400 ) . send ( 'Utilisateur introuvable.' ) ;
120106 }
121107 const promises = Object . keys ( user . data ( ) ?. ownedPromos ) . map ( promoId => db . collection ( promoCollection ) . doc ( promoId ) . get ( ) ) ;
122108 Promise . all ( promises ) . then ( promosSnapshot => {
123- const promotions : any [ ] = [ ] ;
109+ const promotions : Promotion [ ] = [ ] ;
124110 promosSnapshot . forEach ( promo => {
125111 if ( promo . exists ) {
126112 promotions . push ( {
@@ -130,35 +116,32 @@ app.get('/users/:userId/promotions', (req, res) => {
130116 } ) ;
131117 }
132118 } ) ;
133- res . status ( 200 ) . json ( {
134- status : "success" ,
135- data : promotions
136- } ) ;
119+ res . status ( 200 ) . json ( promotions ) ;
137120 } ) . catch ( error => res . status ( 500 ) . send ( error ) ) ;
138121 } ) . catch ( error => res . status ( 500 ) . send ( error ) ) ;
139122} ) ;
140123
141124app . put ( '/users/:userId' , async ( req , res ) => {
142125 const body = req . body ;
143126 if ( ! body . hasOwnProperty ( 'promo' ) ) {
144- res . status ( 400 ) . json ( { error : 'Mauvais argument.' } ) ;
127+ res . status ( 418 ) . send ( 'Mauvais argument.' ) ;
145128 } else {
146129 db . collection ( promoCollection ) . doc ( body . promo ) . get ( ) . then ( promo => {
147130 if ( ! promo . exists ) {
148- res . status ( 400 ) . json ( { error : 'Cette promotion n\'existe pas.' } ) ;
131+ res . status ( 404 ) . send ( 'Cette promotion n\'existe pas.' ) ;
149132 } else {
150133 const dateExpiration = promo . data ( ) ?. dateExpiration . toDate ( ) ;
151134 const dateNow = new Date ( ) ;
152135 if ( dateNow > dateExpiration ) {
153- res . status ( 400 ) . json ( { error : 'Cette promotion est expirée.' } ) ;
136+ res . status ( 418 ) . send ( 'Cette promotion est expirée.' ) ;
154137 } else {
155138 db . collection ( userCollection ) . doc ( req . params . userId ) . get ( ) . then ( user => {
156139 if ( ! user . exists ) {
157- res . status ( 400 ) . json ( { error : 'Cette utilisateur n\'existe pas.' } ) ;
140+ res . status ( 404 ) . send ( 'Cette utilisateur n\'existe pas.' ) ;
158141 } else {
159142 const promos = Object . keys ( user . data ( ) ?. ownedPromos ) ;
160143 if ( promos . includes ( body . promo ) ) {
161- res . status ( 400 ) . json ( { error : 'Vous détenez déjà cette promotion.' } ) ;
144+ res . status ( 418 ) . send ( 'Vous détenez déjà cette promotion.' ) ;
162145 } else {
163146 db . collection ( userCollection ) . doc ( req . params . userId ) . set ( {
164147 ownedPromos : {
@@ -174,5 +157,4 @@ app.put('/users/:userId', async (req, res) => {
174157 }
175158 } ) . catch ( error => res . status ( 500 ) . send ( error ) ) ;
176159 }
177-
178160} ) ;
0 commit comments