Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch only latest open PR from DB #769

Merged
merged 6 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/middleware/notificationOnEditHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class NotificationOnEditHandler {
const site = await this.sitesService.getBySiteName(siteName)
const users = await this.collaboratorsService.list(siteName, userId)
if (site.isErr()) throw new Error("Site should always exist")
const reviewRequests = await this.reviewRequestService.listReviewRequest(
const reviewRequests = await this.reviewRequestService.listValidReviewRequests(
userWithSiteSessionData,
site.value
)
Expand Down
18 changes: 11 additions & 7 deletions src/routes/v2/authenticated/__tests__/review.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("Review Requests Router", () => {
getComments: jest.fn(),
getFullReviewRequest: jest.fn(),
getReviewRequest: jest.fn(),
listReviewRequest: jest.fn(),
listValidReviewRequests: jest.fn(),
markAllReviewRequestsAsViewed: jest.fn(),
markReviewRequestAsViewed: jest.fn(),
mergeReviewRequest: jest.fn(),
Expand Down Expand Up @@ -336,7 +336,7 @@ describe("Review Requests Router", () => {
const mockReviews = ["review1", "review2"]

mockCollaboratorsService.getRole.mockResolvedValueOnce("role")
mockReviewRequestService.listReviewRequest.mockResolvedValueOnce(
mockReviewRequestService.listValidReviewRequests.mockResolvedValueOnce(
mockReviews
)

Expand All @@ -348,9 +348,9 @@ describe("Review Requests Router", () => {
expect(response.body).toEqual({ reviews: mockReviews })
expect(mockSitesService.getBySiteName).toHaveBeenCalledTimes(1)
expect(mockCollaboratorsService.getRole).toHaveBeenCalledTimes(1)
expect(mockReviewRequestService.listReviewRequest).toHaveBeenCalledTimes(
1
)
expect(
mockReviewRequestService.listValidReviewRequests
).toHaveBeenCalledTimes(1)
})

it("should return 404 if the site does not exist", async () => {
Expand All @@ -368,7 +368,9 @@ describe("Review Requests Router", () => {
expect(response.status).toEqual(404)
expect(mockSitesService.getBySiteName).toHaveBeenCalledTimes(1)
expect(mockCollaboratorsService.getRole).not.toHaveBeenCalled()
expect(mockReviewRequestService.listReviewRequest).not.toHaveBeenCalled()
expect(
mockReviewRequestService.listValidReviewRequests
).not.toHaveBeenCalled()
})

it("should return 404 if user is not a site collaborator", async () => {
Expand All @@ -383,7 +385,9 @@ describe("Review Requests Router", () => {
expect(response.status).toEqual(404)
expect(mockSitesService.getBySiteName).toHaveBeenCalledTimes(1)
expect(mockCollaboratorsService.getRole).toHaveBeenCalledTimes(1)
expect(mockReviewRequestService.listReviewRequest).not.toHaveBeenCalled()
expect(
mockReviewRequestService.listValidReviewRequests
).not.toHaveBeenCalled()
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/routes/v2/authenticated/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export class ReviewsRouter {
}

// Step 3: Fetch data and return
const reviews = await this.reviewRequestService.listReviewRequest(
const reviews = await this.reviewRequestService.listValidReviewRequests(
userWithSiteSessionData,
site.value
)
Expand Down
20 changes: 17 additions & 3 deletions src/services/review/ReviewRequestService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AxiosResponse } from "axios"
import _ from "lodash"
import { errAsync, okAsync, ResultAsync } from "neverthrow"
import { ModelStatic } from "sequelize"
import { ModelStatic, Op } from "sequelize"

import UserWithSiteSessionData from "@classes/UserWithSiteSessionData"

Expand Down Expand Up @@ -217,16 +217,25 @@ export default class ReviewRequestService {
return pullRequestNumber
}

listReviewRequest = async (
listValidReviewRequests = async (
sessionData: UserWithSiteSessionData,
site: Site
): Promise<DashboardReviewRequestDto[]> => {
const { siteName, isomerUserId: userId } = sessionData

// Find all review requests associated with the site
const requests = await this.repository.findAll({
// TODO: Note this needs to be findAll when we reach a stage of allowing
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
// multiple open PRs simultaneously
// Current behaviour returns the newest Open PR based on created_at field
const request = await this.repository.findOne({
where: {
siteId: site.id,
[Op.or]: [
{
reviewStatus: ReviewRequestStatus.Open,
},
{ reviewStatus: ReviewRequestStatus.Approved },
],
},
include: [
{
Expand All @@ -238,8 +247,13 @@ export default class ReviewRequestService {
as: "requestor",
},
],
order: [["created_at", "DESC"]],
})

// NOTE: Doing this so that we can easily change back to using
// findAll once ready
const requests = request ? [request] : []

// NOTE: This has a max of 30 pull requests
// and returns only open pull requests.
return Promise.all(
Expand Down
30 changes: 15 additions & 15 deletions src/services/review/__tests__/ReviewRequestService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ describe("ReviewRequestService", () => {
firstView: true,
},
]
MockReviewRequestRepository.findAll.mockResolvedValueOnce([
MOCK_REVIEW_REQUEST_ONE,
])
MockReviewRequestRepository.findOne.mockResolvedValueOnce(
MOCK_REVIEW_REQUEST_ONE
)
MockReviewApi.getPullRequest.mockResolvedValueOnce(MOCK_PULL_REQUEST_ONE)
MockReviewRequestViewRepository.count.mockResolvedValueOnce(0)
MockReviewApi.getComments.mockResolvedValueOnce([
Expand All @@ -547,14 +547,14 @@ describe("ReviewRequestService", () => {
})

// Act
const actual = await ReviewRequestService.listReviewRequest(
const actual = await ReviewRequestService.listValidReviewRequests(
mockUserWithSiteSessionData,
mockSiteOrmResponseWithAllCollaborators as Attributes<Site>
)

// Assert
expect(actual).toEqual(expected)
expect(MockReviewRequestRepository.findAll).toHaveBeenCalled()
expect(MockReviewRequestRepository.findOne).toHaveBeenCalled()
expect(MockReviewApi.getPullRequest).toHaveBeenCalledWith(
mockUserWithSiteSessionData.siteName,
MOCK_REVIEW_REQUEST_ONE.reviewMeta.pullRequestNumber
Expand Down Expand Up @@ -588,9 +588,9 @@ describe("ReviewRequestService", () => {
firstView: false,
},
]
MockReviewRequestRepository.findAll.mockResolvedValueOnce([
MOCK_REVIEW_REQUEST_ONE,
])
MockReviewRequestRepository.findOne.mockResolvedValueOnce(
MOCK_REVIEW_REQUEST_ONE
)
MockReviewApi.getPullRequest.mockResolvedValueOnce(MOCK_PULL_REQUEST_ONE)
MockReviewRequestViewRepository.count.mockResolvedValueOnce(1)
MockReviewApi.getComments.mockResolvedValueOnce([
Expand All @@ -608,14 +608,14 @@ describe("ReviewRequestService", () => {
})

// Act
const actual = await ReviewRequestService.listReviewRequest(
const actual = await ReviewRequestService.listValidReviewRequests(
mockUserWithSiteSessionData,
mockSiteOrmResponseWithAllCollaborators as Attributes<Site>
)

// Assert
expect(actual).toEqual(expected)
expect(MockReviewRequestRepository.findAll).toHaveBeenCalled()
expect(MockReviewRequestRepository.findOne).toHaveBeenCalled()
expect(MockReviewApi.getPullRequest).toHaveBeenCalledWith(
mockUserWithSiteSessionData.siteName,
MOCK_REVIEW_REQUEST_ONE.reviewMeta.pullRequestNumber
Expand Down Expand Up @@ -649,9 +649,9 @@ describe("ReviewRequestService", () => {
firstView: false,
},
]
MockReviewRequestRepository.findAll.mockResolvedValueOnce([
MOCK_REVIEW_REQUEST_ONE,
])
MockReviewRequestRepository.findOne.mockResolvedValueOnce(
MOCK_REVIEW_REQUEST_ONE
)
MockReviewApi.getPullRequest.mockResolvedValueOnce(MOCK_PULL_REQUEST_ONE)
MockReviewRequestViewRepository.count.mockResolvedValueOnce(1)
MockReviewApi.getComments.mockResolvedValueOnce([])
Expand All @@ -660,14 +660,14 @@ describe("ReviewRequestService", () => {
)

// Act
const actual = await ReviewRequestService.listReviewRequest(
const actual = await ReviewRequestService.listValidReviewRequests(
mockUserWithSiteSessionData,
mockSiteOrmResponseWithAllCollaborators as Attributes<Site>
)

// Assert
expect(actual).toEqual(expected)
expect(MockReviewRequestRepository.findAll).toHaveBeenCalled()
expect(MockReviewRequestRepository.findOne).toHaveBeenCalled()
expect(MockReviewApi.getPullRequest).toHaveBeenCalledWith(
mockUserWithSiteSessionData.siteName,
MOCK_REVIEW_REQUEST_ONE.reviewMeta.pullRequestNumber
Expand Down