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(deleagtion-api): Create seed for general mandate delegations #16721

Merged
merged 3 commits into from
Nov 6, 2024

Conversation

GunnlaugurG
Copy link
Member

@GunnlaugurG GunnlaugurG commented Nov 5, 2024

What

Create a seed script that inserts couple of general mandate delegations between gervimenn

Why

So the delegation will exist on staging

Screenshots / Gifs

Attach Screenshots / Gifs to help reviewers understand the scope of the pull request

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Formatting passes locally with my changes
  • I have rebased against main before asking for a review

Summary by CodeRabbit

  • New Features

    • Introduced general mandate delegations to enhance user permissions management.
  • Bug Fixes

    • Improved database transaction handling for delegation entries to ensure data integrity.

@GunnlaugurG GunnlaugurG requested review from a team as code owners November 5, 2024 12:19
Copy link
Contributor

coderabbitai bot commented Nov 5, 2024

Walkthrough

This pull request introduces a new migration file that adds general mandate delegations to the database. It includes an up method for inserting records into the delegation and delegation_delegation_type tables using transactions, and a down method for deleting those records. The migration ensures that all operations are transactional, enabling rollback in case of errors.

Changes

File Path Change Summary
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js Added up and down methods for migrating general mandate delegations, including transaction management and cascading deletes.

Possibly related PRs

Suggested labels

automerge

Suggested reviewers

  • saevarma
  • HjorturJ

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 36.54%. Comparing base (3724852) to head (9294574).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #16721   +/-   ##
=======================================
  Coverage   36.53%   36.54%           
=======================================
  Files        6890     6890           
  Lines      143640   143639    -1     
  Branches    40926    40926           
=======================================
  Hits        52486    52486           
+ Misses      91154    91153    -1     
Flag Coverage Δ
api 3.37% <ø> (ø)
api-domains-auth-admin 48.48% <ø> (ø)
application-system-api 41.11% <ø> (ø)
application-template-api-modules 27.68% <ø> (-0.02%) ⬇️
services-auth-admin-api 52.47% <ø> (ø)
services-auth-delegation-api 58.20% <ø> (-0.07%) ⬇️
services-auth-ids-api 52.08% <ø> (+<0.01%) ⬆️
services-auth-personal-representative 45.58% <ø> (+<0.01%) ⬆️
services-auth-personal-representative-public 41.78% <ø> (ø)
services-auth-public-api 49.58% <ø> (ø)
services-user-notification 46.84% <ø> (ø)
services-user-profile 61.83% <ø> (ø)

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

see 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3724852...9294574. Read the comment docs.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (3)
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js (3)

1-2: Remove redundant 'use strict' directive

JavaScript modules are automatically in strict mode, making this directive unnecessary.

-'use strict'
-
🧰 Tools
🪛 Biome

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)


8-32: Document test data and consider using environment variables

The hardcoded national IDs and names should be documented and potentially moved to environment variables or a separate configuration file for better maintainability.

Consider adding a comment block explaining the test data:

/**
 * Test Data Mapping:
 * - 0101302399: Gervimaður Færeyjar (Faroe Islands test user)
 * - 0101302989: Gervimaður Ameríku (American test user)
 * - 0101305069: Gervimaður Bandaríkin (US test user)
 * - 0101304929: Gervimaður Bretland (UK test user)
 */

54-56: Consider using parameterized values for the WHERE clause

The WHERE clause could be more maintainable using parameterized values instead of hardcoded strings.

-        WHERE to_national_id = '0101302399' AND from_national_id = '0101302989' AND domain_name is null
-        OR to_national_id = '0101305069' AND from_national_id = '0101304929' AND domain_name is null;
+        WHERE (to_national_id, from_national_id) IN (
+          ('0101302399', '0101302989'),
+          ('0101305069', '0101304929')
+        ) AND domain_name is null;
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2d97d44 and e18e9ae.

📒 Files selected for processing (1)
  • libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🪛 Biome
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)

@datadog-island-is
Copy link

datadog-island-is bot commented Nov 5, 2024

Datadog Report

All test runs 0544f3d 🔗

13 Total Test Services: 0 Failed, 13 Passed
🔻 Test Sessions change in coverage: 1 decreased (-0.19%), 1 increased (+0.01%), 22 no change

Test Services
This report shows up to 10 services
Service Name Failed Known Flaky New Flaky Passed Skipped Total Time Code Coverage Change Test Service View
api 0 0 0 4 0 2.58s 1 no change Link
api-domains-auth-admin 0 0 0 18 0 12.12s 1 no change Link
application-system-api 0 0 0 120 2 3m 28.1s 1 no change Link
application-template-api-modules 0 0 0 123 0 2m 25.2s 1 no change Link
auth-api-lib 0 0 0 20 0 33.85s 1 no change Link
services-auth-admin-api 0 0 0 130 0 3m 5.02s 1 no change Link
services-auth-delegation-api 0 0 0 260 0 3m 5.12s 1 decreased (-0.19%) Link
services-auth-ids-api 0 0 0 152 0 1m 2.26s 1 increased (+0.01%) Link
services-auth-personal-representative 0 0 0 59 0 1m 10.52s 1 no change Link
services-auth-personal-representative-public 0 0 0 10 0 27.8s 1 no change Link

🔻 Code Coverage Decreases vs Default Branch (1)

  • services-auth-delegation-api - jest 51.72% (-0.19%) - Details

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js (3)

1-1: Remove redundant 'use strict' directive

The 'use strict' directive is redundant as JavaScript modules are automatically in strict mode.

-'use strict'
🧰 Tools
🪛 Biome

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)


17-32: Document test data purpose and relationships

The test data uses specific national IDs and reference IDs without clear documentation of their purpose or relationships.

Add comments explaining the test scenarios:

           VALUES (
+            -- Test Case 1: Delegation between Gervimaður Færeyjar and Gervimaður Ameríku
             uuid_generate_v4(),
             '0101302399',
             'Gervimaður Færeyjar',
             '0101302989',
             'Gervimaður Ameríku',
             null,
             123
           ), (
+            -- Test Case 2: Delegation between Gervimaður Bandaríkin and Gervimaður Bretland
             uuid_generate_v4(),
             '0101305069',
             'Gervimaður Bandaríkin',
             '0101304929',
             'Gervimaður Bretland',
             null,
             345
           );

55-57: Improve SQL WHERE clause formatting

The WHERE clause could be more readable with proper formatting and parentheses.

         DELETE FROM delegation
-        WHERE to_national_id = '0101302399' AND from_national_id = '0101302989' AND domain_name is null
-        OR to_national_id = '0101305069' AND from_national_id = '0101304929' AND domain_name is null;
+        WHERE (
+            (to_national_id = '0101302399' AND from_national_id = '0101302989' AND domain_name is null)
+            OR 
+            (to_national_id = '0101305069' AND from_national_id = '0101304929' AND domain_name is null)
+        );
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e18e9ae and 578e09b.

📒 Files selected for processing (1)
  • libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🪛 Biome
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)

🔇 Additional comments (1)
libs/auth-api-lib/seeders/20241105120627-add-general-mandate-delegations.js (1)

38-43: 🛠️ Refactor suggestion

Add foreign key existence validation

The insertion into delegation_delegation_type assumes 'GeneralMandate' exists in the delegation_types table. Consider adding a validation check.

           INSERT INTO delegation_delegation_type (
               delegation_id,
               delegation_type_id
           ) VALUES (
+              -- Verify GeneralMandate exists
+              (SELECT CASE 
+                  WHEN EXISTS (SELECT 1 FROM delegation_type WHERE id = 'GeneralMandate')
+                  THEN (select id from delegation where to_national_id = '0101302399' AND from_national_id = '0101302989' AND domain_name is null)
+                  ELSE RAISE EXCEPTION 'GeneralMandate type does not exist'
+               END),
-              (select id from delegation where to_national_id = '0101302399' AND from_national_id = '0101302989' AND domain_name is null),
               'GeneralMandate'
           ), (
+              (SELECT CASE 
+                  WHEN EXISTS (SELECT 1 FROM delegation_type WHERE id = 'GeneralMandate')
+                  THEN (select id from delegation where to_national_id = '0101305069' AND from_national_id = '0101304929' AND domain_name is null)
+                  ELSE RAISE EXCEPTION 'GeneralMandate type does not exist'
+               END),
-              (select id from delegation where to_national_id = '0101305069' AND from_national_id = '0101304929' AND domain_name is null),
               'GeneralMandate'
           );

Copy link
Contributor

@snaerseljan snaerseljan left a comment

Choose a reason for hiding this comment

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

Nice job. One question about the down query

@GunnlaugurG GunnlaugurG added the deprecated:automerge (Disabled) Merge this PR as soon as all checks pass label Nov 6, 2024
@kodiakhq kodiakhq bot merged commit 8bdbd53 into main Nov 6, 2024
49 checks passed
@kodiakhq kodiakhq bot deleted the feat/gm-delegation-seed branch November 6, 2024 09:31
robertaandersen pushed a commit that referenced this pull request Nov 11, 2024
…6721)

* seed to create general mandate delegation between gervimenn

* added transaction to the seed script

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deprecated:automerge (Disabled) Merge this PR as soon as all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants