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

Change return type of QrCode query to better indicate errors #1512

Merged
merged 22 commits into from
Nov 7, 2024

Conversation

pylipp
Copy link
Contributor

@pylipp pylipp commented Sep 9, 2024

The unions may contain error types with detailed info. I hope the following table clarifies how to deal with the response of the query https://github.com/boxwise/boxtribute/blob/change-qrcode-query-interface/front/src/queries/queries.ts#L38-L66

Union fragment Only set if ... Related current response Current handling in FE
1. QrCodeResult.InsufficientPermissionError ...user misses the qr:read permission (e.g. a label-creation user) error code FORBIDDEN message "You don't have permission to access this box!"
2. QrCodeResult.ResourceDoesNotExistError ...the QR code for the requested hash does not exist in the database error code BAD_USER_INPUT message "No box found for this QR code!"
3. QrCodeResult.QrCode ...the errors 1.&2. don't occur QR code data success; redirect accordingly
3a. QrCodeResult.BoxResult.InsufficientPermissionError ...if 3. happens AND user misses the stock:read permission like in 1. like in 1.
3b. QrCodeResult.BoxResult.UnauthorizedForBaseError ...if 3. happens AND user has stock:read permission BUT NOT for the base that the requested box is in like in 1. like in 1.
3c. QrCodeResult.BoxResult.Box ...if the errors 3a. and 3b. don't occur box data (null if QrCode is not associated with any box) success

Note how currently the cases 1, 3a, 3b cannot be distinguished.

Also with the new interface design now in case of 3b information about the base/org of the box can be displayed.

@pylipp pylipp force-pushed the change-qrcode-query-interface branch 2 times, most recently from 564cafe to f7b634e Compare September 9, 2024 14:50
The unions may contain error types with detailed info
@pylipp pylipp force-pushed the change-qrcode-query-interface branch from f7b634e to c1c8ecc Compare September 9, 2024 14:51
@pylipp pylipp force-pushed the change-qrcode-query-interface branch from c1c8ecc to 7ee08fb Compare September 9, 2024 15:01
@pylipp
Copy link
Contributor Author

pylipp commented Sep 9, 2024

@fhenrich33 I consider this completed from a BE perspective. Please check the first post about the different return types of the qrCode query and the QrCode.box field. Overall, the logic in useQrResolver needs a major rework now 😅 let me know how it goes for you. I could already adjust queries.ts though.
Since the changes in the GraphQL API break FE, I suggest you continue to work on it on the same branch, what do you think?

@pylipp pylipp force-pushed the change-qrcode-query-interface branch from a7170c1 to 93beadb Compare September 9, 2024 15:52
@pylipp pylipp changed the title Change QrCode query return type to better indicate errors Change return type of QrCode query to better indicate errors Sep 9, 2024
@fhenrich33
Copy link
Contributor

@fhenrich33 I consider this completed from a BE perspective. Please check the first post about the different return types of the qrCode query and the QrCode.box field. Overall, the logic in useQrResolver needs a major rework now 😅 let me know how it goes for you. I could already adjust queries.ts though. Since the changes in the GraphQL API break FE, I suggest you continue to work on it on the same branch, what do you think?

Makes sense, will do!

Copy link
Member

@HaGuesto HaGuesto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pylipp! Great PR until now.

code review

It passes, but some FYI for @fhenrich33 :

  • Philipp changed the input variable name for qrCode and qrExists query. We should check the places where we execute the query.

functional review

passes as well. This is the query I used to test:

{
  qrExists(code: "093f65e080a295f8076b1c5722a46aa2")
  qrCodeFail: qrCode(code: "093f65e080a295f8076b1c5722a46aa2") {
    ... on QrCode {
      id
      box {
        ... on Box {
          labelIdentifier
        }
        ... on InsufficientPermissionError {
          name
        }
        ... on UnauthorizedForBaseError {
          name
          organisationName
        }
      }
    }
    ... on InsufficientPermissionError {
      name
    }
    ... on InsufficientPermissionError {
      name
    }
  }
  qrCodeValid: qrCode (code: "9d7f34fbea04de63b0acdfb874f3ef59") {
    ... on QrCode {
      id
      box {
        ... on Box {
          labelIdentifier
        }
        ... on InsufficientPermissionError {
          name
        }
        ... on UnauthorizedForBaseError {
          name
          organisationName
        }
      }
    }
    ... on InsufficientPermissionError {
      name
    }
    ... on InsufficientPermissionError {
      name
    }
  }
}

@fhenrich33 fhenrich33 marked this pull request as ready for review October 25, 2024 03:10
Copy link

codecov bot commented Oct 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.33%. Comparing base (5caa2d9) to head (2388dd8).
Report is 24 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1512      +/-   ##
==========================================
- Coverage   84.35%   84.33%   -0.02%     
==========================================
  Files         236      236              
  Lines       14775    14823      +48     
  Branches     1579     1582       +3     
==========================================
+ Hits        12463    12501      +38     
- Misses       2273     2283      +10     
  Partials       39       39              
Flag Coverage Δ
backend 99.12% <ø> (+<0.01%) ⬆️
frontend 77.96% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

front/src/hooks/useQrResolver.ts Outdated Show resolved Hide resolved
front/src/hooks/useQrResolver.ts Outdated Show resolved Hide resolved
front/src/hooks/useQrResolver.ts Outdated Show resolved Hide resolved
front/src/hooks/useQrResolver.ts Outdated Show resolved Hide resolved
front/src/hooks/useQrResolver.ts Show resolved Hide resolved
front/src/hooks/useQrResolver.ts Show resolved Hide resolved
front/src/components/QrReader/QrReaderContainer.tsx Outdated Show resolved Hide resolved
front/src/components/QrReader/QrReaderContainer.tsx Outdated Show resolved Hide resolved
@pylipp
Copy link
Contributor Author

pylipp commented Nov 7, 2024

@fhenrich33 I'll merge master into this branch and try to resolve the conflict in the mutations.graphql file.

// @ts-ignore
if (!data?.qrCode?.box) {

if (data.qrCode.__typename === "ResourceDoesNotExistError") {
return {
kind: IQrResolverResultKind.NOT_ASSIGNED_TO_BOX,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be NO_BOXTRIBUTE_QR and also show an error toast?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is handled at /front/src/components/QrReader/QrReaderContainer.tsx

case IQrResolverResultKind.NOT_ASSIGNED_TO_BOX: {
  if (!multiScan) {
    onSuccess();
    navigate(`/bases/${baseId}/boxes/create/${qrResolvedValue?.qrHash}`);
  } else {
    triggerError({
      message: "No box associated to this QR code!",
    });
    setIsProcessingQrCodeDelayed(false);
  }
  break;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it works 👍 But I end up on an empty page http://localhost:3000/bases/1/boxes/create/13f12820c8010f2f7349962930e6bf4 (I used the last one of the invalid QR codes from the repo readme).
I'd expect to stay on /qrread

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, this should've been handled at front/src/hooks/useQrResolver.ts

const resolveQrCode = useCallback(
  async (qrCodeUrl: string, fetchPolicy: FetchPolicy): Promise<IQrResolvedValue> => {
    setLoading(true);
    const extractedQrHashFromUrl = extractQrCodeFromUrl(qrCodeUrl);

    if (!extractedQrHashFromUrl) {
      triggerError({
        message: "This is not a Boxtribute QR code!",
      });

      setLoading(false);
      return { kind: IQrResolverResultKind.NOT_BOXTRIBUTE_QR } as IQrResolvedValue;
    }

    const qrResolvedValue: IQrResolvedValue = await resolveQrHash(
      extractedQrHashFromUrl,
      fetchPolicy,
    );

    setLoading(false);
    return qrResolvedValue;
  },
  [resolveQrHash, triggerError],
);

Cheking this out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pylipp does this happen in the live version?

@fhenrich33
Copy link
Contributor

@pylipp By the way the tests that are failing doesn't reflect what actually happens in the real app, I didn't figure out a way to fix them yet. cc @HaGuesto

The failing tests:

         3.4.3.5 QrReaderMultiBox.test.tsx
            AssertionError: expected "spy" to be called with arguments: [ ObjectContaining{…} ]

            Received:

            1st spy call:

            Array [
            -   ObjectContaining {
            -     "message": StringMatching /have permission to access this box/i,
            +   Object {
            +     "message": "QR code lookup failed. Please wait a bit and try again.",
                },
            ]
        3.4.8.7 ResolveHash.test.tsx
            TestingLibraryElementError: Unable to find an element by: [data-testid="ReturnScannedQr"]
        3.4.2.2 QrReaderOverlay.test.tsx
            TestingLibraryElementError: Unable to find role="heading" and name "/bases/1/boxes/123"
        3.4.2.5b QrReaderOverlay.test.tsx 
            AssertionError: expected "spy" to be called with arguments: [ ObjectContaining{…} ]

            Received:

            1st spy call:

            Array [
            -   ObjectContaining {
            -     "message": StringMatching /No box found for this QR code/i,
            +   Object {
            +     "message": "QR code lookup failed. Please wait a bit and try again.",
                },
            ]

@HaGuesto
Copy link
Member

HaGuesto commented Nov 7, 2024

@pylipp this PR is ready to be merge. Pls do so when you think it is the right time.

@fhenrich33 fhenrich33 merged commit 69f9971 into master Nov 7, 2024
11 checks passed
@fhenrich33 fhenrich33 deleted the change-qrcode-query-interface branch November 7, 2024 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants