Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
Add create change service APIs (#900)
Browse files Browse the repository at this point in the history
* Add create change service APIs

* Move Change service and graphql into ChangeLogs

* Remove extra changes

* Add TODO
  • Loading branch information
rohithbalaji123 authored Jun 28, 2020
1 parent 67a1087 commit 4f3d635
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
2 changes: 2 additions & 0 deletions frontend/src/service/Captcha.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const UPDATE_SHORT_LINK = 'updateShortLink';
export const SEARCH_SHORT_LINK = 'searchShortLink';
export const VIEW_CHANGE_LOG = 'viewChangeLog';

export const CREATE_CHANGE = 'createChange';

const INVALID_SITE_KEY_ERR_MSG = 'Invalid site key or not loaded in api.js';

export class CaptchaService {
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/service/ChangeLog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ export class ChangeLogService {

return changes;
}

createChange(title: string, summaryMarkdown: string): Promise<Change> {
return new Promise<Change>((resolve, reject) => {
this.changeLogGraphQLApi
.createChange(title, summaryMarkdown)
.then(resolve)
.catch(errCode => {
// TODO(issue#904): impose definite error handling mechanism in client classes.
if (errCode === Err.Unauthenticated) {
reject({ authenticationErr: 'User is not authenticated' });
return;
}
reject({ changeErr: this.errorService.getErr(errCode) });
});
});
}
}
10 changes: 10 additions & 0 deletions frontend/src/service/Error.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum Err {
AliasInvalid = 'invalidCustomAlias',
UserNotHuman = 'requesterNotHuman',
Unauthenticated = 'invalidAuthToken',
Unauthorized = 'unauthorizedAction',
NetworkError = 'networkError',
Unknown = 'unknownError'
}
Expand Down Expand Up @@ -70,6 +71,13 @@ const networkErr = {
`
};

const unauthorizedErr = {
name: 'Unauthorized error',
description: `
You don't have permission needed to perform this action.
`
};

export class ErrorService {
getErr(errCode: Err): IErr {
switch (errCode) {
Expand All @@ -85,6 +93,8 @@ export class ErrorService {
return invalidReCaptchaSiteKeyErr;
case Err.MaliciousContent:
return maliciousContentErr;
case Err.Unauthorized:
return unauthorizedErr;
case Err.Unknown:
default:
return unknownErr;
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/service/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function gqlErrorToCode(graphQLError: IGraphQLError): string {
switch (graphQLError.extensions.code) {
case Err.Unauthenticated:
return Err.Unauthenticated;
case Err.Unauthorized:
return Err.Unauthorized;
default:
return Err.Unknown;
}
Expand Down
50 changes: 49 additions & 1 deletion frontend/src/service/shortGraphQL/ChangeLogGraphQL.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { GraphQLService, IGraphQLRequestError } from '../GraphQL.service';
import { ChangeLog } from '../../entity/ChangeLog';
import { Change } from '../../entity/Change';
import { getErrorCodes } from '../GraphQLError';
import { CaptchaService, VIEW_CHANGE_LOG } from '../Captcha.service';
import {
CaptchaService,
CREATE_CHANGE,
VIEW_CHANGE_LOG
} from '../Captcha.service';
import {
IShortGraphQLChange,
IShortGraphQLChangeLog,
Expand Down Expand Up @@ -98,6 +102,50 @@ export class ChangeLogGraphQLApi {
});
}

async createChange(title: string, summary: string): Promise<Change> {
let captchaResponse;
try {
captchaResponse = await this.captchaService.execute(CREATE_CHANGE);
} catch (err) {
return Promise.reject(err);
}

const createChangeMutation = `
mutation params(
$authToken: String!
$captchaResponse: String!
$change: ChangeInput!
) {
authMutation(authToken: $authToken, captchaResponse: $captchaResponse) {
createChange(change: $change) {
id
title
summaryMarkdown
releasedAt
}
}
}
`;
const variables = {
captchaResponse,
authToken: this.authService.getAuthToken(),
change: { title: title, summaryMarkdown: summary }
};

return new Promise<Change>((resolve, reject) => {
this.graphQLService
.mutate<IShortGraphQLMutation>(this.baseURL, {
mutation: createChangeMutation,
variables: variables
})
.then(res => resolve(this.parseChange(res.authMutation.createChange)))
.catch(err => {
const errCodes = getErrorCodes(err);
reject(errCodes[0]);
});
});
}

private parseChangeLog(changeLog: IShortGraphQLChangeLog): ChangeLog {
if (changeLog.lastViewedAt) {
return {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/service/shortGraphQL/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IShortGraphQLAuthQuery {
export interface IShortGraphQLAuthMutation {
createShortLink: IShortGraphQLShortLink;
viewChangeLog: string;
createChange: IShortGraphQLChange;
}

export interface IShortGraphQLShortLink {
Expand Down

0 comments on commit 4f3d635

Please sign in to comment.