aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/ServiceController.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/Controllers/Http/ServiceController.js')
-rw-r--r--app/Controllers/Http/ServiceController.js103
1 files changed, 51 insertions, 52 deletions
diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js
index 90055b6..a1d26cb 100644
--- a/app/Controllers/Http/ServiceController.js
+++ b/app/Controllers/Http/ServiceController.js
@@ -86,7 +86,7 @@ class ServiceController {
86 const services = (await auth.user.services().fetch()).rows; 86 const services = (await auth.user.services().fetch()).rows;
87 // Convert to array with all data Franz wants 87 // Convert to array with all data Franz wants
88 const servicesArray = services.map((service) => { 88 const servicesArray = services.map((service) => {
89 const settings = typeof service.settings === "string" ? JSON.parse(service.settings) : service.settings; 89 const settings = typeof service.settings === 'string' ? JSON.parse(service.settings) : service.settings;
90 90
91 return { 91 return {
92 customRecipe: false, 92 customRecipe: false,
@@ -105,7 +105,7 @@ class ServiceController {
105 name: service.name, 105 name: service.name,
106 recipeId: service.recipeId, 106 recipeId: service.recipeId,
107 userId: auth.user.id, 107 userId: auth.user.id,
108 } 108 };
109 }); 109 });
110 110
111 return response.send(servicesArray); 111 return response.send(servicesArray);
@@ -127,7 +127,7 @@ class ServiceController {
127 // Upload custom service icon 127 // Upload custom service icon
128 const icon = request.file('icon', { 128 const icon = request.file('icon', {
129 types: ['image'], 129 types: ['image'],
130 size: '2mb' 130 size: '2mb',
131 }); 131 });
132 const { 132 const {
133 id, 133 id,
@@ -135,17 +135,17 @@ class ServiceController {
135 const service = (await Service.query() 135 const service = (await Service.query()
136 .where('serviceId', id) 136 .where('serviceId', id)
137 .where('userId', auth.user.id).fetch()).rows[0]; 137 .where('userId', auth.user.id).fetch()).rows[0];
138 const settings = typeof service.settings === "string" ? JSON.parse(service.settings) : service.settings; 138 const settings = typeof service.settings === 'string' ? JSON.parse(service.settings) : service.settings;
139 139
140 let iconId; 140 let iconId;
141 do { 141 do {
142 iconId = uuid() + uuid(); 142 iconId = uuid() + uuid();
143 } while(await fs.exists(path.join(Helpers.tmpPath('uploads'), iconId))); 143 } while (await fs.exists(path.join(Helpers.tmpPath('uploads'), iconId)));
144 144
145 await icon.move(Helpers.tmpPath('uploads'), { 145 await icon.move(Helpers.tmpPath('uploads'), {
146 name: iconId, 146 name: iconId,
147 overwrite: true 147 overwrite: true,
148 }) 148 });
149 149
150 if (!icon.moved()) { 150 if (!icon.moved()) {
151 return response.status(500).send(icon.error()); 151 return response.status(500).send(icon.error());
@@ -175,49 +175,48 @@ class ServiceController {
175 iconUrl: `${Env.get('APP_URL')}/v1/icon/${newSettings.iconId}`, 175 iconUrl: `${Env.get('APP_URL')}/v1/icon/${newSettings.iconId}`,
176 userId: auth.user.id, 176 userId: auth.user.id,
177 }, 177 },
178 status: ["updated"] 178 status: ['updated'],
179 }); 179 });
180 } else { 180 }
181 // Update service info 181 // Update service info
182 const data = request.all(); 182 const data = request.all();
183 const { 183 const {
184 id, 184 id,
185 } = params; 185 } = params;
186 186
187 // Get current settings from db 187 // Get current settings from db
188 const serviceData = (await Service.query() 188 const serviceData = (await Service.query()
189 .where('serviceId', id) 189 .where('serviceId', id)
190 .where('userId', auth.user.id).fetch()).rows[0]; 190 .where('userId', auth.user.id).fetch()).rows[0];
191 191
192 const settings = { 192 const settings = {
193 ...typeof serviceData.settings === "string" ? JSON.parse(serviceData.settings) : serviceData.settings, 193 ...typeof serviceData.settings === 'string' ? JSON.parse(serviceData.settings) : serviceData.settings,
194 ...data, 194 ...data,
195 }; 195 };
196 196
197 // Update data in database 197 // Update data in database
198 await (Service.query() 198 await (Service.query()
199 .where('serviceId', id) 199 .where('serviceId', id)
200 .where('userId', auth.user.id)).update({ 200 .where('userId', auth.user.id)).update({
201 name: data.name, 201 name: data.name,
202 settings: JSON.stringify(settings), 202 settings: JSON.stringify(settings),
203 }); 203 });
204 204
205 // Get updated row 205 // Get updated row
206 const service = (await Service.query() 206 const service = (await Service.query()
207 .where('serviceId', id) 207 .where('serviceId', id)
208 .where('userId', auth.user.id).fetch()).rows[0]; 208 .where('userId', auth.user.id).fetch()).rows[0];
209 209
210 return response.send({ 210 return response.send({
211 data: { 211 data: {
212 id, 212 id,
213 name: service.name, 213 name: service.name,
214 ...settings, 214 ...settings,
215 iconUrl: `${Env.get('APP_URL')}/v1/icon/${settings.iconId}`, 215 iconUrl: `${Env.get('APP_URL')}/v1/icon/${settings.iconId}`,
216 userId: auth.user.id, 216 userId: auth.user.id,
217 }, 217 },
218 status: ["updated"] 218 status: ['updated'],
219 }); 219 });
220 }
221 } 220 }
222 221
223 async icon({ 222 async icon({
@@ -231,7 +230,7 @@ class ServiceController {
231 const iconPath = path.join(Helpers.tmpPath('uploads'), id); 230 const iconPath = path.join(Helpers.tmpPath('uploads'), id);
232 if (!await fs.exists(iconPath)) { 231 if (!await fs.exists(iconPath)) {
233 return response.status(404).send({ 232 return response.status(404).send({
234 status: 'Icon doesn\'t exist' 233 status: 'Icon doesn\'t exist',
235 }); 234 });
236 } 235 }
237 236
@@ -252,7 +251,7 @@ class ServiceController {
252 .where('userId', auth.user.id).fetch()).rows[0]; 251 .where('userId', auth.user.id).fetch()).rows[0];
253 252
254 const settings = { 253 const settings = {
255 ...typeof serviceData.settings === "string" ? JSON.parse(serviceData.settings) : serviceData.settings, 254 ...typeof serviceData.settings === 'string' ? JSON.parse(serviceData.settings) : serviceData.settings,
256 order: data[service], 255 order: data[service],
257 }; 256 };
258 257
@@ -260,16 +259,16 @@ class ServiceController {
260 await (Service.query() // eslint-disable-line no-await-in-loop 259 await (Service.query() // eslint-disable-line no-await-in-loop
261 .where('serviceId', service) 260 .where('serviceId', service)
262 .where('userId', auth.user.id)) 261 .where('userId', auth.user.id))
263 .update({ 262 .update({
264 settings: JSON.stringify(settings), 263 settings: JSON.stringify(settings),
265 }); 264 });
266 } 265 }
267 266
268 // Get new services 267 // Get new services
269 const services = (await auth.user.services().fetch()).rows; 268 const services = (await auth.user.services().fetch()).rows;
270 // Convert to array with all data Franz wants 269 // Convert to array with all data Franz wants
271 const servicesArray = services.map((service) => { 270 const servicesArray = services.map((service) => {
272 const settings = typeof service.settings === "string" ? JSON.parse(service.settings) : service.settings; 271 const settings = typeof service.settings === 'string' ? JSON.parse(service.settings) : service.settings;
273 272
274 return { 273 return {
275 customRecipe: false, 274 customRecipe: false,
@@ -288,7 +287,7 @@ class ServiceController {
288 name: service.name, 287 name: service.name,
289 recipeId: service.recipeId, 288 recipeId: service.recipeId,
290 userId: auth.user.id, 289 userId: auth.user.id,
291 } 290 };
292 }); 291 });
293 292
294 return response.send(servicesArray); 293 return response.send(servicesArray);