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

Add delete client services #906

Merged
merged 1 commit into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/service/Captcha.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const SEARCH_SHORT_LINK = 'searchShortLink';
export const VIEW_CHANGE_LOG = 'viewChangeLog';

export const CREATE_CHANGE = 'createChange';
export const DELETE_CHANGE = 'deleteChange';

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

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 @@ -89,4 +89,20 @@ export class ChangeLogService {
});
});
}

deleteChange(changeId: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.changeLogGraphQLApi
.deleteChange(changeId)
.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) });
});
});
}
}
40 changes: 40 additions & 0 deletions frontend/src/service/shortGraphQL/ChangeLogGraphQL.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getErrorCodes } from '../GraphQLError';
import {
CaptchaService,
CREATE_CHANGE,
DELETE_CHANGE,
VIEW_CHANGE_LOG
} from '../Captcha.service';
import {
Expand Down Expand Up @@ -146,6 +147,45 @@ export class ChangeLogGraphQLApi {
});
}

async deleteChange(id: string): Promise<string> {
let captchaResponse;
try {
captchaResponse = await this.captchaService.execute(DELETE_CHANGE);
} catch (err) {
return Promise.reject(err);
}

const deleteChangeMutation = `
mutation params(
$authToken: String!
$captchaResponse: String!
$id: String!
) {
authMutation(authToken: $authToken, captchaResponse: $captchaResponse) {
deleteChange(id: $id)
}
}
`;
const variables = {
captchaResponse,
authToken: this.authService.getAuthToken(),
id
};

return new Promise<string>((resolve, reject) => {
this.graphQLService
.mutate<IShortGraphQLMutation>(this.baseURL, {
mutation: deleteChangeMutation,
variables: variables
})
.then(res => resolve(res.authMutation.deleteChange))
.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 @@ -15,6 +15,7 @@ export interface IShortGraphQLAuthMutation {
createShortLink: IShortGraphQLShortLink;
viewChangeLog: string;
createChange: IShortGraphQLChange;
deleteChange: string;
}

export interface IShortGraphQLShortLink {
Expand Down