[TIL] HW5 Refactoring-2
04/27/23
![[TIL] HW5 Refactoring-2](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1682593676271%2F055237ab-dd30-4527-b99d-8687e461bfbf.png&w=3840&q=75)
TIL
Issues encountered
When I used
await Posts.increment("like", { where: { postId }}, { transaction: t });, thelikePOST request kept spinning in the ThunderClient and never properly processed. I'm not sure what the reason is.When I tried to get the refresh token from Redis using
get <refreshToken>, it kept returning undefined.The controller code became too long as I handled the business logic in it, and I also used transactions in the controller.
What I tried
In the existing code, the reason why thunderClient was stuck in a spinning state and not processing normally when calling the like POST was that the transaction object was passed as the third argument to the increment method, instead of the second argument where the transaction object should be passed. Therefore, it should be written as follows:
await Posts.increment("like", { where: { postId }, transaction: t });When trying to retrieve the userId by using
get <refreshToken>in bash to access Redis cloud, the set operation worked fine but there was an error in retrieving the value stored in the cloud. The reason for this was that an asynchronous function was used incorrectly. When initializing the Redis connection,{legacyMode: true}was used to set it to use callback functions for version 3, so in order to use promise functions,redisClient.v4.get()had to be used separately. Because the v4 indicator was not added, undefined was output when trying to retrieve the value using get() before accessing Redis.This issue occurred during the process of reissuing the access token, but after adding v4, the userId value was retrieved correctly and the connection could be established using the reissued access token.
Using the Nullish Coalescing (??) allowed the new token to be passed to
getAccessTokenPayload()without using an if statement.
if (!isAccessTokenValid) {
const accessTokenId = await redisClient.getRefreshToken(authRefreshToken);
if (!accessTokenId)
return res.status(419).json({
message: "Refresh Token does not exist in the server.",
});
newAccessToken = createAccessToken(accessTokenId);
res.cookie("accessToken", `Bearer ${newAccessToken}`);
}
const { userId } = getAccessTokenPayload(newAccessToken ?? authAccessToken);
Business logic should be processed in the service layer, so all the logic used in the controller was passed to the service layer. In the controller, only the validation of input data and one function that meets the purpose of the API were declared. All other logic was processed in the service layer.
In addition, the transaction used in the like POST API was moved to the repository layer.
In the repository layer (likePost function), two operations were processed: inserting a row into the like table and incrementing the posts table's like attribute.
In the service layer, only the likePost function was defined, and 1 was returned for registering a like, and 0 was returned for canceling a like.
In the controller, different messages were output according to the return value of the likePost function.다.
What I newly learned
I fixed the parts that I did wrong for the 3-layered architecture (I put transaction and business logic in the controller)
If token verification fails, I added the code that remove cookie
res.clearCookie('accessToken');
What to learn next
- TDD using
jest
![[코테] 그리디 문제 - 무지의 먹방 라이브](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[코테] Bfs 토마토](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[코테] Dfs 문제 유형 - 그래프 내에서 구분하여 카운트 하기](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[코테] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
![[코테] 여행경로](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)