Skip to content

Commit

Permalink
fix: Load billing md after admin login.
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgcramos authored Nov 22, 2021
1 parent cc333db commit e9bb7b6
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/components/HeaderNav/HeaderNav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ const HeaderNav = ({ history }) => {
className={styles.navLink}
activeClassName={styles.activeNavLink}
to="/user/login">
<Text className={styles.navLinkText}>Log in</Text>
<Text className={styles.navLinkText} data-testid="nav-login">
Log in
</Text>
</NavLink>
<NavLink
className={styles.navLink}
Expand Down
57 changes: 42 additions & 15 deletions src/hooks/api/useProposalsBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ export default function useProposalsBatch({
);
const hasRemainingTokens = !isEmpty(remainingTokens);

const missingBillingStatusChangesTokens = useMemo(() => {
if (!isAdmin || !isStatusApproved) return [];
return tokens.filter((token) =>
isEmpty(billingStatusChangesByToken[shortRecordToken(token)])
);
}, [isAdmin, isStatusApproved, tokens, billingStatusChangesByToken]);

const scanNextStatusTokens = (index, oldTokens) => {
const status = currentStatuses[index];
const newTokens = getUnfetchedTokens(
Expand Down Expand Up @@ -245,28 +252,44 @@ export default function useProposalsBatch({
return send(FETCH);
},
verify: () => {
if (!hasRemainingTokens) return send(RESOLVE, { proposals });
if (!hasRemainingTokens && !missingBillingStatusChangesTokens.length)
return send(RESOLVE, { proposals });
const [tokensToFetch, next] = getTokensForProposalsPagination(
remainingTokens,
proposalPageSize
);
// If fetch tokens of approved proposals and current user is admin
// fetch the billing status changes metadata if doesn't exist in
// the redux store.
let missingBillingStatusChangesTokens;
// If proposals are loaded but there are still missing billing status
// changes. It happens when you have a proposal list loaded and then, as
// admin, navigate to approved proposals tab.
if (missingBillingStatusChangesTokens.length && !tokensToFetch.length) {
const [billingsToFetch] = getTokensForProposalsPagination(
missingBillingStatusChangesTokens,
proposalPageSize
);
onFetchBillingStatusChanges(billingsToFetch)
.then(() => send(VERIFY))
.catch((e) => send(REJECT, e));
return send(FETCH);
}
// If fetch tokens of approved proposals and current user is admin, and
// there are no billing status changes for the tokensToFetch, fetch the
// billing status changes metadata if doesn't exist in the redux store.
let missingBatchBillingStatusChangesTokens;
if (isStatusApproved && isAdmin) {
missingBillingStatusChangesTokens = tokensToFetch.filter((token) =>
isEmpty(billingStatusChangesByToken[shortRecordToken(token)])
missingBatchBillingStatusChangesTokens = tokensToFetch.filter(
(token) =>
isEmpty(billingStatusChangesByToken[shortRecordToken(token)])
);
}
Promise.all([
missingBillingStatusChangesTokens?.length &&
onFetchBillingStatusChanges(missingBillingStatusChangesTokens),
onFetchProposalsBatch({
tokens: tokensToFetch,
fetchVoteSummary,
fetchProposalSummary
})
missingBatchBillingStatusChangesTokens?.length &&
onFetchBillingStatusChanges(missingBatchBillingStatusChangesTokens),
tokensToFetch &&
onFetchProposalsBatch({
tokens: tokensToFetch,
fetchVoteSummary,
fetchProposalSummary
})
])
.then(([, [fetchedProposals]]) => {
if (isEmpty(fetchedProposals)) {
Expand Down Expand Up @@ -301,7 +324,11 @@ export default function useProposalsBatch({

return send(FETCH);
},
done: () => {}
done: () => {
if (missingBillingStatusChangesTokens.length) {
return send(VERIFY);
}
}
},
initialValues: {
status: "idle",
Expand Down
33 changes: 33 additions & 0 deletions teste2e/cypress/e2e/proposal/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,36 @@ describe("Additional page content", () => {
cy.findByTestId("sidebar").should("be.visible");
});
});

describe("Given some previously loaded approved proposals", () => {
beforeEach(() => {
cy.ticketvoteMiddleware("inventory", {
fixedInventory: {
vetted: {
approved: ["token01", "token02", "token03", "token04", "token05"]
}
}
});
cy.ticketvoteMiddleware("summaries", { amountByStatus: { approved: 5 } });
cy.piMiddleware("billingstatuschanges", {
amountByStatus: { 3: 5 },
billingChangesAmount: 0
});
cy.recordsMiddleware("records", { status: 2, state: 2 });
cy.intercept("/api/v1/login", (req) => {
req.reply({});
});
});
it("should fetch the billing changes after admin login", () => {
cy.visit("/?tab=approved");
cy.wait("@records.records");
cy.userEnvironment("admin");
cy.findByTestId("nav-login").click();
cy.findByLabelText(/email/i).type("[email protected]");
cy.findByLabelText(/password/i).type("123123123");
cy.findByTestId("login-form-button").click();
cy.findByTestId("tab-1").click();
cy.wait(1000);
cy.get("@pi.billingstatuschanges.all").should("have.length", 1);
});
});
3 changes: 2 additions & 1 deletion teste2e/cypress/support/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ export function recordsReply({
* @return Inventory map: `{ [state]: { [status]: token } }`
*/
export function inventoryReply({
testParams: { amountByStatus = {}, pageLimit = 20 },
testParams: { amountByStatus = {}, pageLimit = 20, fixedInventory },
requestParams: { state = 2, page }
}) {
if (fixedInventory) return fixedInventory;
const inventory = new Inventory(amountByStatus, {
page,
pageLimit
Expand Down
7 changes: 5 additions & 2 deletions teste2e/cypress/support/pi/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chunkByStatusAmount } from "../core/utils";
import { BillingStatusChanges, Summary } from "./generate";
import times from "lodash/fp/times";

export const API_BASE_URL = "/api/pi/v1";

Expand Down Expand Up @@ -85,7 +86,7 @@ export function policyReply() {
* @returns {Object} summaries map
*/
export function billingStatusChangesReply({
testParams: { amountByStatus },
testParams: { amountByStatus, billingChangesAmount = 1 },
requestParams: { tokens = [] }
}) {
const tokensByStatus = chunkByStatusAmount(tokens, amountByStatus);
Expand All @@ -95,7 +96,9 @@ export function billingStatusChangesReply({
...tokens.reduce(
(sum, token) => ({
...sum,
[token]: [new BillingStatusChanges({ status, token })]
[token]: times(() => new BillingStatusChanges({ status, token }))(
billingChangesAmount
)
}),
{}
)
Expand Down

0 comments on commit e9bb7b6

Please sign in to comment.