#20 Return person on change role mutation
Return the whole person object on calling the changeRole mutation. This enables the apollo cache in the frontend to automatically update the role of the given user. Furthermore, made all create function statements idempotent by deleting them first if existing.
This commit is contained in:
parent
1e518e8e9d
commit
4a2ba8406d
|
@ -1,6 +1,8 @@
|
||||||
create extension if not exists "pgcrypto";
|
create extension if not exists "pgcrypto";
|
||||||
|
|
||||||
|
|
||||||
-- Define JWT claim structure
|
-- Define JWT claim structure
|
||||||
|
drop type if exists candymat_data.jwt_token cascade;
|
||||||
create type candymat_data.jwt_token as
|
create type candymat_data.jwt_token as
|
||||||
(
|
(
|
||||||
role text,
|
role text,
|
||||||
|
@ -8,6 +10,9 @@ create type candymat_data.jwt_token as
|
||||||
exp bigint
|
exp bigint
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
-- Function to get the currently logged-in person
|
||||||
|
drop function if exists candymat_data.current_person;
|
||||||
create function candymat_data.current_person(
|
create function candymat_data.current_person(
|
||||||
) returns candymat_data.person as
|
) returns candymat_data.person as
|
||||||
$$
|
$$
|
||||||
|
@ -17,6 +22,9 @@ where row_id = nullif(current_setting('jwt.claims.person_row_id', true), '')::in
|
||||||
$$ language sql stable;
|
$$ language sql stable;
|
||||||
grant execute on function candymat_data.current_person() to candymat_person;
|
grant execute on function candymat_data.current_person() to candymat_person;
|
||||||
|
|
||||||
|
|
||||||
|
-- Function to register a new user
|
||||||
|
drop function candymat_data.register_person;
|
||||||
create function candymat_data.register_person(
|
create function candymat_data.register_person(
|
||||||
first_name text,
|
first_name text,
|
||||||
last_name text,
|
last_name text,
|
||||||
|
@ -47,9 +55,11 @@ begin
|
||||||
end ;
|
end ;
|
||||||
$$ language plpgsql strict
|
$$ language plpgsql strict
|
||||||
security definer;
|
security definer;
|
||||||
|
|
||||||
grant execute on function candymat_data.register_person(text, text, text, text) to candymat_anonymous;
|
grant execute on function candymat_data.register_person(text, text, text, text) to candymat_anonymous;
|
||||||
|
|
||||||
|
|
||||||
|
-- Authenticate: Login for user
|
||||||
|
drop function if exists candymat_data.authenticate;
|
||||||
create function candymat_data.authenticate(
|
create function candymat_data.authenticate(
|
||||||
email text,
|
email text,
|
||||||
password text
|
password text
|
||||||
|
@ -80,26 +90,24 @@ $$ language plpgsql strict
|
||||||
security definer;
|
security definer;
|
||||||
grant execute on function candymat_data.authenticate(text, text) to candymat_anonymous, candymat_person;
|
grant execute on function candymat_data.authenticate(text, text) to candymat_anonymous, candymat_person;
|
||||||
|
|
||||||
|
|
||||||
|
-- Change role: Changes role for a given user. Only editors are allowed to use it.
|
||||||
|
drop function if exists candymat_data.change_role;
|
||||||
create function candymat_data.change_role(
|
create function candymat_data.change_role(
|
||||||
person_row_id integer,
|
person_row_id integer,
|
||||||
new_role candymat_data.role
|
new_role candymat_data.role
|
||||||
)
|
)
|
||||||
returns table
|
returns candymat_data.person as
|
||||||
(
|
|
||||||
first_name text,
|
|
||||||
last_name text,
|
|
||||||
role candymat_data.role
|
|
||||||
)
|
|
||||||
as
|
|
||||||
$$
|
$$
|
||||||
|
declare
|
||||||
|
person candymat_data.person;
|
||||||
begin
|
begin
|
||||||
update candymat_data.person
|
update candymat_data.person
|
||||||
set role = new_role
|
set role = new_role
|
||||||
where candymat_data.person.row_id = $1;
|
where candymat_data.person.row_id = $1
|
||||||
|
returning * into person;
|
||||||
|
|
||||||
return query select candymat_data.person.first_name::text, candymat_data.person.last_name::text, new_role
|
return person;
|
||||||
from candymat_data.person
|
|
||||||
where person.row_id = person_row_id;
|
|
||||||
end;
|
end;
|
||||||
$$ language plpgsql;
|
$$ language plpgsql strict security definer;
|
||||||
grant execute on function candymat_data.change_role(integer, candymat_data.role) to candymat_editor;
|
grant execute on function candymat_data.change_role(integer, candymat_data.role) to candymat_editor;
|
||||||
|
|
|
@ -1,29 +1,26 @@
|
||||||
import { gql } from "@apollo/client";
|
import { gql } from "@apollo/client";
|
||||||
|
import { UppercaseUserRole } from "../../jwt/jwt";
|
||||||
|
import { BasicPersonFragment, BasicPersonResponse } from "../queries/person";
|
||||||
|
|
||||||
export const ChangeRole = gql`
|
export const ChangeRole = gql`
|
||||||
mutation ChangeRole (
|
mutation ChangeRole($personRowId: Int!, $newRole: Role!) {
|
||||||
$firstName: String!
|
changeRole(input: { personRowId: $personRowId, newRole: $newRole }) {
|
||||||
$lastName: String!
|
person {
|
||||||
$role: String!
|
...BasicPersonFragment
|
||||||
){
|
}
|
||||||
__typename
|
|
||||||
changeRole(input: {newRole: CANDYMAT_EDITOR, personRowId: 3}) {
|
|
||||||
results {
|
|
||||||
firstName
|
|
||||||
lastName
|
|
||||||
role
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
${BasicPersonFragment}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export interface ChangeRoleVariables {
|
export interface ChangeRoleVariables {
|
||||||
email: string;
|
personRowId: number;
|
||||||
password: string;
|
newRole: UppercaseUserRole;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ChangeRoleResponse {
|
export interface ChangeRoleResponse {
|
||||||
authenticate: {
|
__typename: "Mutation";
|
||||||
jwtToken?: string;
|
changeRole: {
|
||||||
|
person?: BasicPersonResponse;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { gql } from "@apollo/client";
|
import { gql } from "@apollo/client";
|
||||||
import { UppercaseUserRole } from "../../jwt/jwt";
|
import { UppercaseUserRole } from "../../jwt/jwt";
|
||||||
|
|
||||||
const BasicPersonFragment = gql`
|
export const BasicPersonFragment = gql`
|
||||||
fragment BasicPersonFragment on Person {
|
fragment BasicPersonFragment on Person {
|
||||||
id
|
id
|
||||||
rowId
|
rowId
|
||||||
|
|
Loading…
Reference in a new issue