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;