Skip to content

Commit

Permalink
feat(core): Add updateActiveAdministrator mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 19, 2020
1 parent 0df7c71 commit 73ab736
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 18 deletions.
14 changes: 14 additions & 0 deletions packages/admin-ui/src/lib/core/src/common/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ export type Mutation = {
settleRefund: SettleRefundResult;
transitionFulfillmentToState: TransitionFulfillmentToStateResult;
transitionOrderToState?: Maybe<TransitionOrderToStateResult>;
/** Update the active (currently logged-in) Administrator */
updateActiveAdministrator: Administrator;
/** Update an existing Administrator */
updateAdministrator: Administrator;
/** Update an existing Asset */
Expand Down Expand Up @@ -816,6 +818,11 @@ export type MutationTransitionOrderToStateArgs = {
};


export type MutationUpdateActiveAdministratorArgs = {
input: UpdateActiveAdministratorInput;
};


export type MutationUpdateAdministratorArgs = {
input: UpdateAdministratorInput;
};
Expand Down Expand Up @@ -957,6 +964,13 @@ export type UpdateAdministratorInput = {
roleIds?: Maybe<Array<Scalars['ID']>>;
};

export type UpdateActiveAdministratorInput = {
firstName?: Maybe<Scalars['String']>;
lastName?: Maybe<Scalars['String']>;
emailAddress?: Maybe<Scalars['String']>;
password?: Maybe<Scalars['String']>;
};

export type MimeTypeError = ErrorResult & {
__typename?: 'MimeTypeError';
errorCode: ErrorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export type Mutation = {
createAdministrator: Administrator;
/** Update an existing Administrator */
updateAdministrator: Administrator;
/** Update the active (currently logged-in) Administrator */
updateActiveAdministrator: Administrator;
/** Delete an Administrator */
deleteAdministrator: DeletionResponse;
/** Assign a Role to an Administrator */
Expand Down Expand Up @@ -411,6 +413,10 @@ export type MutationUpdateAdministratorArgs = {
input: UpdateAdministratorInput;
};

export type MutationUpdateActiveAdministratorArgs = {
input: UpdateActiveAdministratorInput;
};

export type MutationDeleteAdministratorArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -795,6 +801,13 @@ export type UpdateAdministratorInput = {
roleIds?: Maybe<Array<Scalars['ID']>>;
};

export type UpdateActiveAdministratorInput = {
firstName?: Maybe<Scalars['String']>;
lastName?: Maybe<Scalars['String']>;
emailAddress?: Maybe<Scalars['String']>;
password?: Maybe<Scalars['String']>;
};

export type MimeTypeError = ErrorResult & {
errorCode: ErrorCode;
message: Scalars['String'];
Expand Down
14 changes: 14 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ export type Mutation = {
createAdministrator: Administrator;
/** Update an existing Administrator */
updateAdministrator: Administrator;
/** Update the active (currently logged-in) Administrator */
updateActiveAdministrator: Administrator;
/** Delete an Administrator */
deleteAdministrator: DeletionResponse;
/** Assign a Role to an Administrator */
Expand Down Expand Up @@ -456,6 +458,11 @@ export type MutationUpdateAdministratorArgs = {
};


export type MutationUpdateActiveAdministratorArgs = {
input: UpdateActiveAdministratorInput;
};


export type MutationDeleteAdministratorArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -926,6 +933,13 @@ export type UpdateAdministratorInput = {
roleIds?: Maybe<Array<Scalars['ID']>>;
};

export type UpdateActiveAdministratorInput = {
firstName?: Maybe<Scalars['String']>;
lastName?: Maybe<Scalars['String']>;
emailAddress?: Maybe<Scalars['String']>;
password?: Maybe<Scalars['String']>;
};

export type MimeTypeError = ErrorResult & {
__typename?: 'MimeTypeError';
errorCode: ErrorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Object {
"lastName": "new last",
"user": Object {
"id": "T_3",
"identifier": "[email protected]",
"identifier": "new-email",
"lastLogin": null,
"roles": Array [
Object {
Expand Down
65 changes: 49 additions & 16 deletions packages/core/e2e/administrator.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DeletionResult,
GetAdministrator,
GetAdministrators,
UpdateActiveAdministrator,
UpdateAdministrator,
} from './graphql/generated-e2e-admin-types';
import { CREATE_ADMINISTRATOR } from './graphql/shared-definitions';
Expand Down Expand Up @@ -73,22 +74,6 @@ describe('Administrator resolver', () => {
expect(result.administrator).toEqual(createdAdmin);
});

it('activeAdministrator', async () => {
await adminClient.asAnonymousUser();

const { activeAdministrator: result1 } = await adminClient.query<ActiveAdministrator.Query>(
GET_ACTIVE_ADMINISTRATOR,
);
expect(result1).toBeNull();

await adminClient.asSuperAdmin();

const { activeAdministrator: result2 } = await adminClient.query<ActiveAdministrator.Query>(
GET_ACTIVE_ADMINISTRATOR,
);
expect(result2?.emailAddress).toBe(SUPER_ADMIN_USER_IDENTIFIER);
});

it('updateAdministrator', async () => {
const result = await adminClient.query<UpdateAdministrator.Mutation, UpdateAdministrator.Variables>(
UPDATE_ADMINISTRATOR,
Expand Down Expand Up @@ -175,6 +160,45 @@ describe('Administrator resolver', () => {

expect(administrator).toBeNull();
});

it('activeAdministrator', async () => {
await adminClient.asAnonymousUser();

const { activeAdministrator: result1 } = await adminClient.query<ActiveAdministrator.Query>(
GET_ACTIVE_ADMINISTRATOR,
);
expect(result1).toBeNull();

await adminClient.asSuperAdmin();

const { activeAdministrator: result2 } = await adminClient.query<ActiveAdministrator.Query>(
GET_ACTIVE_ADMINISTRATOR,
);
expect(result2?.emailAddress).toBe(SUPER_ADMIN_USER_IDENTIFIER);
});

it('updateActiveAdministrator', async () => {
const { updateActiveAdministrator } = await adminClient.query<
UpdateActiveAdministrator.Mutation,
UpdateActiveAdministrator.Variables
>(UPDATE_ACTIVE_ADMINISTRATOR, {
input: {
firstName: 'Thomas',
lastName: 'Anderson',
emailAddress: '[email protected]',
},
});

expect(updateActiveAdministrator.firstName).toBe('Thomas');
expect(updateActiveAdministrator.lastName).toBe('Anderson');

const { activeAdministrator } = await adminClient.query<ActiveAdministrator.Query>(
GET_ACTIVE_ADMINISTRATOR,
);

expect(activeAdministrator?.firstName).toBe('Thomas');
expect(activeAdministrator?.user.identifier).toBe('[email protected]');
});
});

export const GET_ADMINISTRATORS = gql`
Expand Down Expand Up @@ -207,6 +231,15 @@ export const GET_ACTIVE_ADMINISTRATOR = gql`
${ADMINISTRATOR_FRAGMENT}
`;

export const UPDATE_ACTIVE_ADMINISTRATOR = gql`
mutation UpdateActiveAdministrator($input: UpdateActiveAdministratorInput!) {
updateActiveAdministrator(input: $input) {
...Administrator
}
}
${ADMINISTRATOR_FRAGMENT}
`;

export const UPDATE_ADMINISTRATOR = gql`
mutation UpdateAdministrator($input: UpdateAdministratorInput!) {
updateAdministrator(input: $input) {
Expand Down
27 changes: 27 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export type Mutation = {
createAdministrator: Administrator;
/** Update an existing Administrator */
updateAdministrator: Administrator;
/** Update the active (currently logged-in) Administrator */
updateActiveAdministrator: Administrator;
/** Delete an Administrator */
deleteAdministrator: DeletionResponse;
/** Assign a Role to an Administrator */
Expand Down Expand Up @@ -411,6 +413,10 @@ export type MutationUpdateAdministratorArgs = {
input: UpdateAdministratorInput;
};

export type MutationUpdateActiveAdministratorArgs = {
input: UpdateActiveAdministratorInput;
};

export type MutationDeleteAdministratorArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -795,6 +801,13 @@ export type UpdateAdministratorInput = {
roleIds?: Maybe<Array<Scalars['ID']>>;
};

export type UpdateActiveAdministratorInput = {
firstName?: Maybe<Scalars['String']>;
lastName?: Maybe<Scalars['String']>;
emailAddress?: Maybe<Scalars['String']>;
password?: Maybe<Scalars['String']>;
};

export type MimeTypeError = ErrorResult & {
errorCode: ErrorCode;
message: Scalars['String'];
Expand Down Expand Up @@ -4014,6 +4027,12 @@ export type ActiveAdministratorQueryVariables = Exact<{ [key: string]: never }>;

export type ActiveAdministratorQuery = { activeAdministrator?: Maybe<AdministratorFragment> };

export type UpdateActiveAdministratorMutationVariables = Exact<{
input: UpdateActiveAdministratorInput;
}>;

export type UpdateActiveAdministratorMutation = { updateActiveAdministrator: AdministratorFragment };

export type UpdateAdministratorMutationVariables = Exact<{
input: UpdateAdministratorInput;
}>;
Expand Down Expand Up @@ -5753,6 +5772,14 @@ export namespace ActiveAdministrator {
export type ActiveAdministrator = NonNullable<ActiveAdministratorQuery['activeAdministrator']>;
}

export namespace UpdateActiveAdministrator {
export type Variables = UpdateActiveAdministratorMutationVariables;
export type Mutation = UpdateActiveAdministratorMutation;
export type UpdateActiveAdministrator = NonNullable<
UpdateActiveAdministratorMutation['updateActiveAdministrator']
>;
}

export namespace UpdateAdministrator {
export type Variables = UpdateAdministratorMutationVariables;
export type Mutation = UpdateAdministratorMutation;
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/api/resolvers/admin/administrator.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MutationAssignRoleToAdministratorArgs,
MutationCreateAdministratorArgs,
MutationDeleteAdministratorArgs,
MutationUpdateActiveAdministratorArgs,
MutationUpdateAdministratorArgs,
Permission,
QueryAdministratorArgs,
Expand Down Expand Up @@ -70,6 +71,22 @@ export class AdministratorResolver {
return this.administratorService.update(ctx, input);
}

@Transaction()
@Mutation()
@Allow(Permission.Owner)
async updateActiveAdministrator(
@Ctx() ctx: RequestContext,
@Args() args: MutationUpdateActiveAdministratorArgs,
): Promise<Administrator | undefined> {
if (ctx.activeUserId) {
const { input } = args;
const administrator = await this.administratorService.findOneByUserId(ctx, ctx.activeUserId);
if (administrator) {
return this.administratorService.update(ctx, { ...input, id: administrator.id });
}
}
}

@Transaction()
@Mutation()
@Allow(Permission.UpdateAdministrator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type Mutation {
createAdministrator(input: CreateAdministratorInput!): Administrator!
"Update an existing Administrator"
updateAdministrator(input: UpdateAdministratorInput!): Administrator!
"Update the active (currently logged-in) Administrator"
updateActiveAdministrator(input: UpdateActiveAdministratorInput!): Administrator!
"Delete an Administrator"
deleteAdministrator(id: ID!): DeletionResponse!
"Assign a Role to an Administrator"
Expand All @@ -34,3 +36,10 @@ input UpdateAdministratorInput {
password: String
roleIds: [ID!]
}

input UpdateActiveAdministratorInput {
firstName: String
lastName: String
emailAddress: String
password: String
}
4 changes: 4 additions & 0 deletions packages/core/src/service/services/administrator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export class AdministratorService {
let updatedAdministrator = patchEntity(administrator, input);
await this.connection.getRepository(ctx, Administrator).save(administrator, { reload: false });

if (input.emailAddress) {
updatedAdministrator.user.identifier = input.emailAddress;
await this.connection.getRepository(ctx, User).save(updatedAdministrator.user);
}
if (input.password) {
const user = await this.userService.getUserById(ctx, administrator.user.id);
if (user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export type Mutation = {
createAdministrator: Administrator;
/** Update an existing Administrator */
updateAdministrator: Administrator;
/** Update the active (currently logged-in) Administrator */
updateActiveAdministrator: Administrator;
/** Delete an Administrator */
deleteAdministrator: DeletionResponse;
/** Assign a Role to an Administrator */
Expand Down Expand Up @@ -411,6 +413,10 @@ export type MutationUpdateAdministratorArgs = {
input: UpdateAdministratorInput;
};

export type MutationUpdateActiveAdministratorArgs = {
input: UpdateActiveAdministratorInput;
};

export type MutationDeleteAdministratorArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -795,6 +801,13 @@ export type UpdateAdministratorInput = {
roleIds?: Maybe<Array<Scalars['ID']>>;
};

export type UpdateActiveAdministratorInput = {
firstName?: Maybe<Scalars['String']>;
lastName?: Maybe<Scalars['String']>;
emailAddress?: Maybe<Scalars['String']>;
password?: Maybe<Scalars['String']>;
};

export type MimeTypeError = ErrorResult & {
errorCode: ErrorCode;
message: Scalars['String'];
Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

0 comments on commit 73ab736

Please sign in to comment.