From 13d11bbbeee9b6e09529417d1aef6d0ee5fea4ba Mon Sep 17 00:00:00 2001 From: Joerg Reichert Date: Sat, 8 Apr 2023 09:44:31 +0200 Subject: [PATCH] apply merge request changes https://github.com/technologiestiftung/giessdenkiez-de/pull/554 --- src/mocks/handlers.ts | 82 +++++++++++++-------- src/utils/requestUtil.ts | 1 + src/utils/requests/adoptTree.ts | 6 +- src/utils/requests/getCommunityData.ts | 10 +-- src/utils/requests/getTreeData.ts | 4 +- src/utils/requests/getTreesAdoptedByUser.ts | 4 +- src/utils/requests/getTreesByIds.ts | 2 +- src/utils/requests/getUserWaterings.ts | 4 +- src/utils/requests/getWateredTrees.ts | 12 --- src/utils/requests/isTreeAdopted.ts | 4 +- src/utils/requests/unadoptTree.ts | 4 +- src/utils/requests/updateWatering.ts | 3 +- src/utils/requests/waterTree.ts | 5 +- 13 files changed, 69 insertions(+), 72 deletions(-) delete mode 100644 src/utils/requests/getWateredTrees.ts diff --git a/src/mocks/handlers.ts b/src/mocks/handlers.ts index b66fdb5f4..b68575432 100644 --- a/src/mocks/handlers.ts +++ b/src/mocks/handlers.ts @@ -63,7 +63,7 @@ export const handlers = [ return res(ctx.status(201)); }), - rest.delete(`${location}/delete`, (req, res, ctx) => { + rest.delete(`${location}/delete/:type`, (req, res, ctx) => { // console.log('intercepting DELETE requests'); const json: Payload = {}; let body: Record = {}; @@ -72,9 +72,18 @@ export const handlers = [ } else { body = req.body ? req.body : {}; } - const queryType = body ? getProperty(body, 'queryType') : ''; + const { type } = req.params; + if (!type) { + return res(ctx.status(400), ctx.json({ message: 'type is undefined' })); + } + if (typeof type !== 'string') { + return res( + ctx.status(400), + ctx.json({ message: 'type is not a string' }) + ); + } // console.log(body); - switch (queryType) { + switch (type) { case 'unadopt': { // remove from adopted trees list // adoptedTreeIds[] @@ -86,7 +95,7 @@ export const handlers = [ } case undefined: case null: { - console.log(' queryType is undefined or null'); + console.log(' type is undefined or null'); break; } default: { @@ -95,7 +104,7 @@ export const handlers = [ } return res(ctx.status(201), ctx.json(json)); }), - rest.post(`${location}/post`, (req, res, ctx) => { + rest.post(`${location}/post/:type`, (req, res, ctx) => { let json: Payload = {}; let body: Record = {}; if (typeof req.body === 'string') { @@ -103,9 +112,17 @@ export const handlers = [ } else { body = req.body ? req.body : {}; } - const queryType = body ? getProperty(body, 'queryType') : ''; - - switch (queryType) { + const { type } = req.params; + if (!type) { + return res(ctx.status(400), ctx.json({ message: 'type is undefined' })); + } + if (typeof type !== 'string') { + return res( + ctx.status(400), + ctx.json({ message: 'type is not a string' }) + ); + } + switch (type) { case 'water': { treesWatered.push({ tree_id: getProperty(body, 'tree_id'), @@ -132,7 +149,7 @@ export const handlers = [ watered: '1', }); } - json = { data: 'watered tree', message: `${queryType}` }; + json = { data: 'watered tree', message: `${type}` }; break; } case 'adopt': { @@ -156,12 +173,12 @@ export const handlers = [ watered: '0', }); } - json = { data: 'tree was adopted', message: `${queryType}` }; + json = { data: 'tree was adopted', message: `${type}` }; break; } case undefined: case null: { - console.error('queryType is not defiend'); + console.error('type url param is not defiend'); break; } default: { @@ -177,17 +194,25 @@ export const handlers = [ return res(ctx.status(200), ctx.json({ foo: 'bar' })); }), - rest.get(`${location}/get`, async (req, res, ctx) => { + rest.get(`${location}/get/:type`, async (req, res, ctx) => { let json: Payload = {}; - // const queries = getUrlQueries(req.url.href); - // const query = req.url.searchParams; - // const queryType = query.get('queryType'); - const queryType = getProperty(req.url.searchParams, 'queryType'); - - switch (queryType) { - case 'treesWateredByUser': { - json = { data: treesWatered, message: `${queryType}` }; + const { type } = req.params as Record; + if (!type) { + return res( + ctx.status(400), + ctx.json({ message: 'url param is missing' }) + ); + } + if (typeof type === 'string') { + return res( + ctx.status(400), + ctx.json({ message: 'url param is not a string' }) + ); + } + switch (type) { + case 'treeswateredbyuser': { + json = { data: treesWatered, message: `${type}` }; break; } @@ -205,7 +230,7 @@ export const handlers = [ break; } case 'adopted': { - json = { data: [...adoptedTreeIds], message: `${queryType}` }; + json = { data: [...adoptedTreeIds], message: `${type}` }; break; } case 'istreeadopted': { @@ -213,17 +238,17 @@ export const handlers = [ json = { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion data: adoptedTreeIds.includes(id) ? true : false, - message: `${queryType}`, + message: `${type}`, }; break; } case 'wateredbyuser': { // const uuid = queries.get('uuid'); // if we need it - json = { data: treesWatered, message: `${queryType}` }; + json = { data: treesWatered, message: `${type}` }; break; } case 'wateredandadopted': { - json = { data: wateredAndAdopted, message: `${queryType}` }; + json = { data: wateredAndAdopted, message: `${type}` }; break; } case 'lastwatered': { @@ -237,12 +262,12 @@ export const handlers = [ } }); const lastWateredByUserFiltered = lastWateredByUser.filter(Boolean); // https://stackoverflow.com/a/281335/1770432 - json = { data: lastWateredByUserFiltered, message: `${queryType}` }; + json = { data: lastWateredByUserFiltered, message: `${type}` }; break; } case 'watered': { const watered = treesWatered.map(tree => tree.tree_id); - json = { data: { watered }, message: `${queryType}` }; + json = { data: { watered }, message: `${type}` }; break; } @@ -253,7 +278,7 @@ export const handlers = [ json = { data: [], url: req.url, - message: `case ${queryType} with url "${req.url}" in default case. Not yet defined and passed through`, + message: `case ${type} with url "${req.url}" in default case. Not yet defined and passed through`, ...originalResponse, }; // console.log('response is patched and gets passed through', json); @@ -267,9 +292,6 @@ export const handlers = [ rest.get( 'https://tsb-trees-api-user-management.now.sh/api/user', (req, res, ctx) => { - // const query = req.url.searchParams; - // const queryType = query.get('queryType'); - // console.log('queryType', queryType); const userid = getProperty(req.url.searchParams, 'id'); const json: Payload = { diff --git a/src/utils/requestUtil.ts b/src/utils/requestUtil.ts index 941ece14c..4fb0438bf 100644 --- a/src/utils/requestUtil.ts +++ b/src/utils/requestUtil.ts @@ -18,6 +18,7 @@ export async function requests< const msg = await response.text(); throw new Error(msg); } + if (response.status === 204) return {} as ReturnType; const json = await response.json(); return json; } catch (err) { diff --git a/src/utils/requests/adoptTree.ts b/src/utils/requests/adoptTree.ts index 077031ad2..c6e329b95 100644 --- a/src/utils/requests/adoptTree.ts +++ b/src/utils/requests/adoptTree.ts @@ -12,18 +12,18 @@ export const adoptTree = async ({ token: string; userId: string; }): Promise => { - const url = createAPIUrl(`/post`); + const url = createAPIUrl(`/post/adopt`); await requests(url, { token, override: { method: 'POST', - body: JSON.stringify({ tree_id: id, uuid: userId, queryType: 'adopt' }), + body: JSON.stringify({ tree_id: id, uuid: userId }), }, }); const res = await requests<{ data: Tree[] }>( - createAPIUrl(`/get?&queryType=byid&id=${id}`) + createAPIUrl(`/get/byid?id=${id}`) ); const tree = res.data[0]; diff --git a/src/utils/requests/getCommunityData.ts b/src/utils/requests/getCommunityData.ts index 5fdda42b4..df2823eba 100644 --- a/src/utils/requests/getCommunityData.ts +++ b/src/utils/requests/getCommunityData.ts @@ -3,15 +3,13 @@ import { createAPIUrl } from '../createAPIUrl'; import { requests } from '../requestUtil'; export const getCommunityData = async (): Promise => { - const fetchCommunityDataUrl = createAPIUrl( - `/get?queryType=wateredandadopted` - ); + const fetchCommunityDataUrl = createAPIUrl(`/get/wateredandadopted`); var json = await requests<{ data: { tree_id: string; - adopted: string; - watered: string; + adopted: number; + watered: number; }[]; }>(fetchCommunityDataUrl); @@ -26,7 +24,7 @@ export const getCommunityData = async (): Promise => { const newState = json.data.reduce( (acc: CommunityDataType, { tree_id: id, adopted, watered }) => { const item = acc[id]; - const isAdopted = item?.adopted || adopted !== '0'; + const isAdopted = item?.adopted || adopted !== 0; const wateredAmount = item?.watered || watered; return { communityFlagsMap: { diff --git a/src/utils/requests/getTreeData.ts b/src/utils/requests/getTreeData.ts index 933da43c8..836c3d546 100644 --- a/src/utils/requests/getTreeData.ts +++ b/src/utils/requests/getTreeData.ts @@ -39,8 +39,8 @@ export const getTreeData = async ( ): Promise<{ selectedTreeData: SelectedTreeType | undefined; }> => { - const urlSelectedTree = createAPIUrl(`/get?queryType=byid&id=${id}`); - const urlWaterings = createAPIUrl(`/get?queryType=lastwatered&id=${id}`); + const urlSelectedTree = createAPIUrl(`/get/byid?id=${id}`); + const urlWaterings = createAPIUrl(`/get/lastwatered?id=${id}`); const [resSelectedTree, resWaterings] = await Promise.all([ requests(urlSelectedTree), diff --git a/src/utils/requests/getTreesAdoptedByUser.ts b/src/utils/requests/getTreesAdoptedByUser.ts index 98eeb2389..7d3fd2fda 100644 --- a/src/utils/requests/getTreesAdoptedByUser.ts +++ b/src/utils/requests/getTreesAdoptedByUser.ts @@ -10,9 +10,7 @@ export const getTreesAdoptedByUser = async ({ userId: string; token: string; }): Promise => { - const urlAdoptedTreesIds = createAPIUrl( - `/get?queryType=adopted&uuid=${userId}` - ); + const urlAdoptedTreesIds = createAPIUrl(`/get/adopted?uuid=${userId}`); const res = await requests<{ data: string[] }>(urlAdoptedTreesIds, { token }); if (!res?.data || res.data.length === 0) return []; const trees = await getTreesByIds(res.data); diff --git a/src/utils/requests/getTreesByIds.ts b/src/utils/requests/getTreesByIds.ts index 832a6d554..f3f7cbf5a 100644 --- a/src/utils/requests/getTreesByIds.ts +++ b/src/utils/requests/getTreesByIds.ts @@ -9,7 +9,7 @@ export const getTreesByIds = async (ids: string[]): Promise => { '' ); - const url = createAPIUrl(`/get/?queryType=treesbyids&tree_ids=${queryStr}`); + const url = createAPIUrl(`/get/treesbyids?tree_ids=${queryStr}`); const res = await requests<{ data: Tree[] }>(url); return res.data; }; diff --git a/src/utils/requests/getUserWaterings.ts b/src/utils/requests/getUserWaterings.ts index 1c509d1b1..21def7221 100644 --- a/src/utils/requests/getUserWaterings.ts +++ b/src/utils/requests/getUserWaterings.ts @@ -10,9 +10,7 @@ export const getUserWaterings = async ({ userId: string; token: string; }): Promise => { - const urlWateredByUser = createAPIUrl( - `/get?queryType=wateredbyuser&uuid=${userId}` - ); + const urlWateredByUser = createAPIUrl(`/get/wateredbyuser?uuid=${userId}`); const res = await requests<{ data: RawWateringType[] }>(urlWateredByUser, { token, }); diff --git a/src/utils/requests/getWateredTrees.ts b/src/utils/requests/getWateredTrees.ts deleted file mode 100644 index d5f277e28..000000000 --- a/src/utils/requests/getWateredTrees.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { createAPIUrl } from '../createAPIUrl'; -import { requests } from '../requestUtil'; - -export const getWateredTrees = async (): Promise => { - const url = createAPIUrl('/get?queryType=watered'); - const result = await requests<{ data?: { watered: string[] } }>(url); - - if (result.data === undefined) { - throw new Error('data is not defined on getWateredTrees'); - } - return result.data.watered; -}; diff --git a/src/utils/requests/isTreeAdopted.ts b/src/utils/requests/isTreeAdopted.ts index e809de342..56d9a992c 100644 --- a/src/utils/requests/isTreeAdopted.ts +++ b/src/utils/requests/isTreeAdopted.ts @@ -14,9 +14,7 @@ export async function isTreeAdopted( ): Promise { const { isAuthenticated, uuid, id, token, signal } = opts; if (!isAuthenticated) return false; - const url = createAPIUrl( - `/get?queryType=istreeadopted&uuid=${uuid}&id=${id}` - ); + const url = createAPIUrl(`/get/istreeadopted?uuid=${uuid}&id=${id}`); const json = await requests< { data: IsTreeAdoptedProps }, diff --git a/src/utils/requests/unadoptTree.ts b/src/utils/requests/unadoptTree.ts index 0e8e805f1..ad6324e82 100644 --- a/src/utils/requests/unadoptTree.ts +++ b/src/utils/requests/unadoptTree.ts @@ -10,9 +10,7 @@ export const unadoptTree = async ({ token: string; userId: string; }): Promise => { - const urlUnadopt = createAPIUrl( - `/delete?tree_id=${id}&uuid=${userId}&queryType=unadopt` - ); + const urlUnadopt = createAPIUrl(`/delete/unadopt`); await requests(urlUnadopt, { token, diff --git a/src/utils/requests/updateWatering.ts b/src/utils/requests/updateWatering.ts index 92587eafe..6ad010f8b 100644 --- a/src/utils/requests/updateWatering.ts +++ b/src/utils/requests/updateWatering.ts @@ -12,13 +12,12 @@ export const updateWatering = async ({ field: string; value: string; }): Promise => { - const urlWaterings = createAPIUrl(`/post`); + const urlWaterings = createAPIUrl(`/post/watering-update`); await requests(urlWaterings, { token, override: { method: 'POST', body: JSON.stringify({ - queryType: 'watering-update', watering_id: wateringId, patches: [{ name: field, diff --git a/src/utils/requests/waterTree.ts b/src/utils/requests/waterTree.ts index deeb3b3d5..0e64a8b37 100644 --- a/src/utils/requests/waterTree.ts +++ b/src/utils/requests/waterTree.ts @@ -16,9 +16,7 @@ export const waterTree = async ({ username: string; token: string; }): Promise => { - const urlPostWatering = createAPIUrl( - `/post?tree_id=${id}&amount=${amount}×tamp=${timestamp}&uuid=${userId}&token=${token}&username=${username}&queryType=water` - ); + const urlPostWatering = createAPIUrl(`/post/water`); await requests(urlPostWatering, { token, @@ -30,7 +28,6 @@ export const waterTree = async ({ timestamp, uuid: userId, username, - queryType: 'water', }), }, });