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

fix: group cleanup #4334

Merged
merged 1 commit into from
Jul 24, 2023
Merged

fix: group cleanup #4334

merged 1 commit into from
Jul 24, 2023

Conversation

kwasniew
Copy link
Contributor

@kwasniew kwasniew commented Jul 24, 2023

About the changes

  • Group API cleanup triggered by the OpenAPI improvements
  • Replacing 500 errors with 400 errors when foreign key violations happen (no role, no user)
  • Separating write/create model and read model for groups
  • Removing transaction boundaries from the store to the controller in group-controller to have the same pattern across the entire codebase and possibly span multiple stores

Important files

Discussion points

@sonatype-lift
Copy link

sonatype-lift bot commented Jul 24, 2023

Sonatype Lift is retiring

Sonatype Lift will be retiring on Sep 12, 2023, with its analysis stopping on Aug 12, 2023. We understand that this news may come as a disappointment, and Sonatype is committed to helping you transition off it seamlessly. If you’d like to retain your data, please export your issues from the web console.
We are extremely grateful and thank you for your support over the years.

📖 Read about the impacts and timeline

@vercel
Copy link

vercel bot commented Jul 24, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 24, 2023 8:33am
unleash-monorepo-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 24, 2023 8:33am

.returning(GROUP_COLUMNS);

return rowToGroup(rows[0]);
} catch (error) {
Copy link
Contributor Author

@kwasniew kwasniew Jul 24, 2023

Choose a reason for hiding this comment

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

Screenshot 2023-07-24 at 09 22 02

What if the role id does not exist? Currently we don't check if the role is a root role, but we don't have this check before either.

Copy link
Member

Choose a reason for hiding this comment

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

If the role doesn't exist at all, it should fail, which I think this now does, thanks to this PR. If the role is not a root role then this will succeed but effectively be a no-op. Probably needs some love but I think this PR takes us forward

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, this PR prevent doesn't allow to pass nonexistent role ids and fails with 400 on those

.returning('*');
return rowToGroup(row[0]);
} catch (error) {
if (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same as update

});
return await this.db.batchInsert(T.GROUP_USER, rows);
} catch (error) {
if (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screenshot 2023-07-24 at 09 22 02 If user list has invalid user id. We could check this upfront when we validate group in the service but it was more invasive since we'd need to inject read models from the user subdomain. I don't mind having this code here since it's DB related code wrapped in a DB specific store so the details are not leaking out.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah agreed, I think it's fine living in this logical layer

import { AccountStore } from '../../db/account-store';
import EventStore from '../../db/event-store';

export const createGroupService = (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

needed for transaction support so that we new up a service per DB transaction

import { FromSchema } from 'json-schema-to-ts';
import { groupSchema } from './group-schema';

export const createGroupSchema = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

separate schema from the read schema we were sharing before

user: {
type: 'object',
description: 'A minimal user object',
required: ['id'],
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 was checking the UI and we don't send full users as the previous schema indicated but only the ids.

@@ -33,6 +33,11 @@ export interface IGroupModel extends IGroup {
projects?: string[];
}

export interface ICreateGroupModel extends IGroup {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

new TS model reflecting openapi schema change. Users are optional and each user is represented by id only

@@ -96,4 +96,5 @@ export interface IUnleashServices {
transactionalFeatureToggleService: (
db: Knex.Transaction,
) => FeatureToggleService;
transactionalGroupService: (db: Knex.Transaction) => GroupService;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we'll use it in the enterprise controller

@@ -38,20 +38,20 @@ export interface IGroupStore extends Store<IGroup, number> {

updateGroupUsers(
groupId: number,
newUsers: IGroupUserModel[],
newUsers: ICreateGroupUserModel[],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

write/create model decoupled from the read model

@@ -124,3 +124,26 @@ test('adding a root role to a group with a project role should fail', async () =

expect.assertions(1);
});

test('adding a nonexistent role to a group should fail', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

confirming the try/catch I added in the store works

created_by: userName,
};
});
return (transaction || this.db).batchInsert(T.GROUP_USER, rows);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removing transactions from the store and moving them to the controller/around the group service in the enterprise repo

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

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

LGTM

@kwasniew kwasniew merged commit 5de4958 into main Jul 24, 2023
19 checks passed
@kwasniew kwasniew deleted the group-openapi-cleanup branch July 24, 2023 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants