|
1 | 1 | const express = require('express');
|
2 | 2 | const admin = require('firebase-admin');
|
3 |
| -const cors = require('cors')({ origin: true }); |
4 |
| -const Busboy = require('busboy'); |
5 |
| -const path = require('path'); |
6 |
| -const os = require('os'); |
7 |
| -const fs = require('fs'); |
8 | 3 | const uuid = require('uuid/v4');
|
9 | 4 |
|
10 | 5 | const router = express.Router();
|
11 | 6 |
|
12 |
| -const { storageBucket } = JSON.parse(process.env.FIREBASE_CONFIG); |
13 |
| - |
14 |
| -const bucket = admin.storage().bucket(storageBucket); |
15 |
| - |
16 |
| -const uploadImageToBucket = async uploadedImage => { |
17 |
| - return bucket.upload(uploadedImage.file, { |
18 |
| - uploadMedia: 'media', |
19 |
| - metada: { |
20 |
| - metadata: { |
21 |
| - contentType: uploadedImage.type |
22 |
| - } |
23 |
| - }, |
24 |
| - public: true |
25 |
| - }); |
26 |
| -}; |
27 |
| - |
28 | 7 | const createUserAuth = async (email, isAdmin) => {
|
29 | 8 | const { uid } = await admin.auth().createUser({ email, password: uuid() });
|
30 | 9 |
|
@@ -53,143 +32,4 @@ router.post('/', async (request, response) => {
|
53 | 32 | return response.status(200).json({ uid });
|
54 | 33 | });
|
55 | 34 |
|
56 |
| -router.delete('/:id', async (request, response) => { |
57 |
| - const { id } = request.params; |
58 |
| - |
59 |
| - const userRef = admin.database().ref(`users/${id}`); |
60 |
| - |
61 |
| - const logoUrl = (await userRef.child('logoUrl').once('value')).val(); |
62 |
| - |
63 |
| - let removeLogo = Promise.resolve(); |
64 |
| - |
65 |
| - if (logoUrl) { |
66 |
| - const fileName = logoUrl.split('/').pop(); |
67 |
| - |
68 |
| - removeLogo = bucket.file(fileName).delete(); |
69 |
| - } |
70 |
| - |
71 |
| - const removeUserDb = userRef.remove(); |
72 |
| - |
73 |
| - const removeUserAuth = admin.auth().deleteUser(id); |
74 |
| - |
75 |
| - try { |
76 |
| - await Promise.all([removeUserDb, removeUserAuth, removeLogo]); |
77 |
| - } catch (error) { |
78 |
| - console.error(error); |
79 |
| - return response.status(500).json({ error }); |
80 |
| - } |
81 |
| - |
82 |
| - return response.status(200).json({}); |
83 |
| -}); |
84 |
| - |
85 |
| -const modifyUserAuth = (userId, isAdmin) => { |
86 |
| - return admin.auth().setCustomUserClaims(userId, { |
87 |
| - isAdmin |
88 |
| - }); |
89 |
| -}; |
90 |
| - |
91 |
| -const modifyUserDb = (name, location, createdAt, isAdmin, logoUrl, userId) => { |
92 |
| - let params = { name, location, createdAt, isAdmin }; |
93 |
| - |
94 |
| - if (logoUrl) params['logoUrl'] = logoUrl; |
95 |
| - |
96 |
| - return admin |
97 |
| - .database() |
98 |
| - .ref(`users/${userId}`) |
99 |
| - .update({ ...params }); |
100 |
| -}; |
101 |
| - |
102 |
| -const removeOldLogo = async userId => { |
103 |
| - const url = ( |
104 |
| - await admin |
105 |
| - .database() |
106 |
| - .ref(`users/${userId}`) |
107 |
| - .child('logoUrl') |
108 |
| - .once('value') |
109 |
| - ).val(); |
110 |
| - |
111 |
| - if (url) { |
112 |
| - const fileName = url.split('/').pop(); |
113 |
| - console.log('filename', fileName); |
114 |
| - return bucket.file(fileName).delete(); |
115 |
| - } else { |
116 |
| - return Promise.resolve(); |
117 |
| - } |
118 |
| -}; |
119 |
| - |
120 |
| -router.patch('/:id', (request, response) => { |
121 |
| - cors(request, response, () => { |
122 |
| - const busboy = new Busboy({ headers: request.headers }); |
123 |
| - |
124 |
| - let uploadedImage = null; |
125 |
| - |
126 |
| - let fieldData = {}; |
127 |
| - |
128 |
| - busboy.on('field', (fieldName, value) => { |
129 |
| - fieldData = { ...fieldData, [`${fieldName}`]: value }; |
130 |
| - }); |
131 |
| - |
132 |
| - busboy.on('file', (fieldName, file, fileName, encoding, mimetype) => { |
133 |
| - const filepath = path.join(os.tmpdir(), fileName); |
134 |
| - |
135 |
| - uploadedImage = { file: filepath, type: mimetype, fileName }; |
136 |
| - |
137 |
| - file.pipe(fs.createWriteStream(filepath)); |
138 |
| - }); |
139 |
| - |
140 |
| - busboy.on('finish', async () => { |
141 |
| - const { name, location, createdAt } = fieldData; |
142 |
| - |
143 |
| - const isAdmin = JSON.parse(fieldData.isAdmin); |
144 |
| - |
145 |
| - const { id } = request.params; |
146 |
| - |
147 |
| - const setUserClaims = modifyUserAuth(id, isAdmin); |
148 |
| - |
149 |
| - let removeLogo = Promise.resolve(); |
150 |
| - let uploadImage = Promise.resolve(); |
151 |
| - |
152 |
| - if (uploadedImage) { |
153 |
| - removeLogo = removeOldLogo(id); |
154 |
| - uploadImage = uploadImageToBucket(uploadedImage); |
155 |
| - } |
156 |
| - |
157 |
| - await removeLogo; |
158 |
| - |
159 |
| - let logoUrl = null; |
160 |
| - |
161 |
| - if (uploadedImage) { |
162 |
| - logoUrl = `https://storage.googleapis.com/${bucket.name}/${uploadedImage.fileName}`; |
163 |
| - } |
164 |
| - |
165 |
| - const modifyUser = modifyUserDb( |
166 |
| - name, |
167 |
| - location, |
168 |
| - createdAt, |
169 |
| - isAdmin, |
170 |
| - logoUrl, |
171 |
| - id |
172 |
| - ); |
173 |
| - |
174 |
| - try { |
175 |
| - await Promise.all([modifyUser, setUserClaims, uploadImage]); |
176 |
| - } catch (error) { |
177 |
| - console.error(error); |
178 |
| - return response.status(500).json({ error }); |
179 |
| - } |
180 |
| - |
181 |
| - return response.status(201).json({ |
182 |
| - id, |
183 |
| - name, |
184 |
| - location, |
185 |
| - logoUrl, |
186 |
| - isAdmin, |
187 |
| - createdAt |
188 |
| - }); |
189 |
| - }); |
190 |
| - |
191 |
| - busboy.end(request.rawBody); |
192 |
| - }); |
193 |
| -}); |
194 |
| - |
195 | 35 | module.exports = router;
|
0 commit comments