Skip to content

Commit 0d3538f

Browse files
author
Pierre-Florent Poujol
committed
api final version & implement put request for adding promo to user's account
1 parent ea8acde commit 0d3538f

File tree

13 files changed

+97
-127
lines changed

13 files changed

+97
-127
lines changed

capacitor.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"appId": "io.ionic.starter",
3-
"appName": "IonicGoStyle",
2+
"appId": "com.epsi.gostyle",
3+
"appName": "GoStyle",
44
"bundledWebRuntime": false,
55
"npmClient": "npm",
66
"webDir": "www",

config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<widget id="com.epsi.gostyle" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
2+
<widget id="com.epsi.gostyle" version="1.1.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
33
<name>GoStyle</name>
44
<description>Scan QR code and get promotions.</description>
55
<author email="pf.poujol@gmail.com" href="http://ionicframework.com/">Pierre-Florent Poujol</author>

functions/package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
},
1515
"main": "lib/index.js",
1616
"dependencies": {
17+
"@types/cors": "^2.8.7",
1718
"body-parser": "^1.19.0",
19+
"cors": "^2.8.5",
1820
"express": "^4.17.1",
1921
"firebase-admin": "^8.10.0",
2022
"firebase-functions": "^3.6.1"

functions/src/index.ts

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import * as functions from 'firebase-functions';
22
import * as admin from 'firebase-admin';
33
import * as express from 'express';
44
import * 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

613
admin.initializeApp(functions.config().firebase);
714

@@ -10,6 +17,8 @@ const app = express();
1017
const 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}));
1322
main.use('/v1', app);
1423
main.use(bodyParser.json());
1524
main.use(bodyParser.urlencoded({extended: false}));
@@ -22,38 +31,24 @@ const promoCollection = 'promotions';
2231
//define google cloud function name
2332
export const webServices = functions.region('europe-west1').https.onRequest(main);
2433

25-
2634
app.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

141124
app.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
});

ionic.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "IonicGoStyle",
2+
"name": "GoStyle",
33
"integrations": {
44
"capacitor": {},
55
"cordova": {}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "IonicGoStyle",
3-
"version": "0.0.1",
3+
"version": "1.1.0",
44
"author": "Ionic Framework",
55
"homepage": "https://ionicframework.com/",
66
"scripts": {
@@ -86,4 +86,4 @@
8686
"browser"
8787
]
8888
}
89-
}
89+
}

0 commit comments

Comments
 (0)