-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DELETE endpoint for UserChangemakerPermission
This endpoint makes it possible for a user with appropriate permissions to remove a changemaker permission for a given user. Issue #1250 Support associations between users and organizational entities
- Loading branch information
Showing
13 changed files
with
482 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/database/operations/userChangemakerPermissions/assertUserChangemakerPermissionExists.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { db } from '../../db'; | ||
import { NotFoundError } from '../../../errors'; | ||
import { keycloakUserIdToString } from '../../../types'; | ||
import type { | ||
CheckResult, | ||
Id, | ||
KeycloakUserId, | ||
Permission, | ||
} from '../../../types'; | ||
|
||
const assertUserChangemakerPermissionExists = async ( | ||
userKeycloakUserId: KeycloakUserId, | ||
changemakerId: Id, | ||
permission: Permission, | ||
): Promise<void> => { | ||
const result = await db.sql<CheckResult>( | ||
'userChangemakerPermissions.checkExistsByPrimaryKey', | ||
{ | ||
userKeycloakUserId, | ||
changemakerId, | ||
permission, | ||
}, | ||
); | ||
|
||
if (result.rows[0]?.result !== true) { | ||
throw new NotFoundError(`Entity not found`, { | ||
entityType: 'UserChangemakerPermission', | ||
entityPrimaryKey: { | ||
userKeycloakUserId: keycloakUserIdToString(userKeycloakUserId), | ||
changemakerId, | ||
permission, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
export { assertUserChangemakerPermissionExists }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from './assertUserChangemakerPermissionExists'; | ||
export * from './createOrUpdateUserChangemakerPermission'; | ||
export * from './loadUserChangemakerPermission'; | ||
export * from './removeUserChangemakerPermission'; |
37 changes: 37 additions & 0 deletions
37
src/database/operations/userChangemakerPermissions/loadUserChangemakerPermission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { db } from '../../db'; | ||
import { NotFoundError } from '../../../errors'; | ||
import { | ||
keycloakUserIdToString, | ||
type Id, | ||
type JsonResultSet, | ||
type KeycloakUserId, | ||
type Permission, | ||
type UserChangemakerPermission, | ||
} from '../../../types'; | ||
|
||
export const loadUserChangemakerPermission = async ( | ||
userKeycloakUserId: KeycloakUserId, | ||
changemakerId: Id, | ||
permission: Permission, | ||
): Promise<UserChangemakerPermission> => { | ||
const result = await db.sql<JsonResultSet<UserChangemakerPermission>>( | ||
'userChangemakerPermissions.selectByPrimaryKey', | ||
{ | ||
userKeycloakUserId, | ||
changemakerId, | ||
permission, | ||
}, | ||
); | ||
const object = result.rows[0]?.object; | ||
if (object === undefined) { | ||
throw new NotFoundError(`Entity not found`, { | ||
entityType: 'UserchangemakerPermission', | ||
entityPrimaryKey: { | ||
userKeycloakUserId: keycloakUserIdToString(userKeycloakUserId), | ||
changemakerId, | ||
permission, | ||
}, | ||
}); | ||
} | ||
return object; | ||
}; |
32 changes: 32 additions & 0 deletions
32
src/database/operations/userChangemakerPermissions/removeUserChangemakerPermission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NotFoundError } from '../../../errors'; | ||
import { keycloakUserIdToString } from '../../../types'; | ||
import { db } from '../../db'; | ||
import type { KeycloakUserId, Permission } from '../../../types'; | ||
|
||
const removeUserChangemakerPermission = async ( | ||
userKeycloakUserId: KeycloakUserId, | ||
changemakerId: number, | ||
permission: Permission, | ||
): Promise<void> => { | ||
const result = await db.sql('userChangemakerPermissions.deleteOne', { | ||
userKeycloakUserId, | ||
permission, | ||
changemakerId, | ||
}); | ||
|
||
if (result.row_count === 0) { | ||
throw new NotFoundError( | ||
Check warning on line 18 in src/database/operations/userChangemakerPermissions/removeUserChangemakerPermission.ts Codecov / codecov/patchsrc/database/operations/userChangemakerPermissions/removeUserChangemakerPermission.ts#L18
|
||
'The item did not exist and could not be deleted.', | ||
{ | ||
entityType: 'UserChangemakerPermission', | ||
entityPrimaryKey: { | ||
userKeycloakUserId: keycloakUserIdToString(userKeycloakUserId), | ||
permission, | ||
changemakerId, | ||
}, | ||
}, | ||
); | ||
} | ||
}; | ||
|
||
export { removeUserChangemakerPermission }; |
8 changes: 8 additions & 0 deletions
8
src/database/queries/userChangemakerPermissions/checkExistsByPrimaryKey.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
SELECT exists( | ||
SELECT 1 | ||
FROM user_changemaker_permissions | ||
WHERE user_keycloak_user_id = :userKeycloakUserId | ||
AND changemaker_id = :changemakerId | ||
AND permission = :permission | ||
AND NOT is_expired(not_after) | ||
) AS result; |
Oops, something went wrong.