From bca8c6cb81fd0bd62170aa9551e8ca0233099844 Mon Sep 17 00:00:00 2001 From: vigdorov Date: Thu, 10 Sep 2020 19:16:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=B7=D0=B0=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B9=20=D0=BD=D0=B5=20=D0=B1=D1=8B=D0=BB=D0=BE=20=D0=B4=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=83=D0=BF=D0=B0=20=D0=BF=D0=BE=20=D1=85=D1=83?= =?UTF-8?q?=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/store.http | 13 ++++++------- src/store/store.controller.ts | 4 ++-- src/store/store.service.ts | 21 +++++++++++++-------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/http/store.http b/http/store.http index 8e47ba2..970d7e3 100644 --- a/http/store.http +++ b/http/store.http @@ -2,14 +2,12 @@ GET http://localhost:4001/store HTTP/1.1 Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6InZpZ2Rvcm92IiwiYWdlbnQiOiJ2c2NvZGUtcmVzdGNsaWVudCIsImlhdCI6MTU5OTQ5OTYyMSwiZXhwIjoxNTk5NDk5OTIxfQ.Ym4xOpoe8UvO626Vxp-XqxspoJSL7JpjZ1rlSW0vIWs - ### GET http://localhost:4001/store/rgfdfff HTTP/1.1 Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6InZpZ2Rvcm92IiwiYWdlbnQiOiJ2c2NvZGUtcmVzdGNsaWVudCIsImlhdCI6MTU5OTQ5OTYyMSwiZXhwIjoxNTk5NDk5OTIxfQ.Ym4xOpoe8UvO626Vxp-XqxspoJSL7JpjZ1rlSW0vIWs - ### -GET http://localhost:4001/store/rgfdfff?hook=sdf6sdfhs99-sdf HTTP/1.1 +GET http://localhost:4001/store/testAp2?hook=c8d5f28b-4460-47b9-9369-01caf4951846 HTTP/1.1 ### POST http://localhost:4001/store HTTP/1.1 @@ -28,14 +26,15 @@ Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6InZpZ2Rvcm92Iiwi } ### Update Request -PUT http://localhost:4001/store HTTP/1.1 +PUT http://localhost:4001/store?hook=c8d5f28b-4460-47b9-9369-01caf4951846 HTTP/1.1 content-type: application/json -Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6ImFkbWluIiwiYWdlbnQiOiJ2c2NvZGUtcmVzdGNsaWVudCIsImlhdCI6MTU5OTQ5OTAxNCwiZXhwIjoxNTk5NDk5MzE0fQ.kpW1KPVGQvI7iZ1-EhyeO5MAGbo4clpQ0J-ategIf2w { - "key": "testAp", + "key": "testAp2", "value": { - + "test": 45, + "test2": 33, + "test3": 13 }, "description": "тестовое апи22", "service_name": "test-api", diff --git a/src/store/store.controller.ts b/src/store/store.controller.ts index f216175..35f2aca 100644 --- a/src/store/store.controller.ts +++ b/src/store/store.controller.ts @@ -71,7 +71,7 @@ export class StoreController { await this.authService.checkRequest(request); } - const store = await this.storeService.findOneStore(api, key, request.headers.authorization); + const store = await this.storeService.findOneStore(api, key, isActualHook, request.headers.authorization); return prepareStoreToStoreRequest(store); } @@ -113,7 +113,7 @@ export class StoreController { await this.authService.checkRequest(request); } - const store = await this.storeService.update(api, request.body, request.headers.authorization); + const store = await this.storeService.update(api, request.body, isActualHook, request.headers.authorization); return prepareStoreToStoreRequest(store); } diff --git a/src/store/store.service.ts b/src/store/store.service.ts index dbbb7b0..7f2548b 100644 --- a/src/store/store.service.ts +++ b/src/store/store.service.ts @@ -115,7 +115,7 @@ export class StoreService { } } - async loadUserByToken (access_token: string): Promise { + async loadUserByToken(access_token: string): Promise { const {login, agent} = jwt.decode(access_token) as Token; const apiPath = 'http://api.auth.vigdorov.ru/users/search/'; const headers = { @@ -124,24 +124,26 @@ export class StoreService { }; try { const {data: currentUser} = await this.http.get(`${apiPath}${login}`, {headers}).toPromise(); - return currentUser; + return currentUser; } catch (e) { if (e?.response?.status === 401) { throw new UnauthorizedException('Доступ запрещен'); } throw new BadRequestException(e.message); } - + } - async update(api: string, {author, ...omitProps}: StoreRequest, access_token: string): Promise { + async update(api: string, {author, ...omitProps}: StoreRequest, isActualHook: boolean, access_token: string): Promise { const searchStore = await this.findOne(api, omitProps.key); if (searchStore) { - const user = await this.loadUserByToken(access_token); + if (!isActualHook) { + const user = await this.loadUserByToken(access_token); - if (!user.is_admin && user.login !== searchStore.author) { - throw new NotFoundException(`Not found api key "${omitProps.key}"`); + if (!user.is_admin && user.login !== searchStore.author) { + throw new NotFoundException(`Not found api key "${omitProps.key}"`); + } } const store = { @@ -177,8 +179,11 @@ export class StoreService { return searchStore; } - async findOneStore(api: string, key: string, access_token: string): Promise { + async findOneStore(api: string, key: string, isActualHook: boolean, access_token: string): Promise { const store = await this.findOne(api, key); + if (isActualHook) { + return store; + } const user = await this.loadUserByToken(access_token); if (user.is_admin || user.login === store.author || !store.hide) { return store;