Skip to content

Commit

Permalink
addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
NiranjanaBinoy committed May 23, 2024
1 parent dde8ae6 commit 4c9ece3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
BaseController,
RestrictedControllerMessenger,
} from '@metamask/base-controller';
import MetaMetricsController from '../metametrics';
import { ObservableStore } from '@metamask/obs-store';
import { MetaMetricsControllerState } from '../metametrics';
import {
createDataDeletionRegulationTask,
fetchDeletionRegulationStatus,
Expand All @@ -11,15 +12,21 @@ import {
// Unique name for the controller
const controllerName = 'MetaMetricsDataDeletionController';

/**
* @type DataDeleteDate
* Deletion date in DD/MM/YYYY format
*/
export type DataDeleteDate = string;
/**
* @type DataDeleteDate
* Regulation Id retuned while creating a delete regulation.
*/
export type DataDeleteRegulationId = string;

export type RegulationId = Record<string, string>;
export type CurrentRegulationStatus = Record<string, Record<string, string>>;
export type DeleteRegulationAPIResponse = {
data: RegulationId | CurrentRegulationStatus;
};

/**
* @type DeleteRegulationStatus
* The status on which to filter the returned regulations.
*/
export enum DeleteRegulationStatus {
FAILED = 'FAILED',
FINISHED = 'FINISHED',
Expand All @@ -30,7 +37,13 @@ export enum DeleteRegulationStatus {
RUNNING = 'RUNNING',
UNKNOWN = 'UNKNOWN',
}

/**
* @type MetaMetricsDataDeletionState
* MetaMetricsDataDeletionController controller state
* @property metaMetricsDataDeletionId - Regulation Id retuned while creating a delete regulation.
* @property metaMetricsDataDeletionDate - Date at which the most recent regulation is created/requested for.
* @property metaMetricsDataDeletionStatus - Status of the current delete regulation.
*/
export type MetaMetricsDataDeletionState = {
metaMetricsDataDeletionId: DataDeleteRegulationId;
metaMetricsDataDeletionDate: DataDeleteDate;
Expand Down Expand Up @@ -66,15 +79,15 @@ export type CreateMetaMetricsDataDeletionTaskAction = {
};

// Describes the action to check teh existing regulation status
export type CheckDataDeletionTaskStatusAction = {
type: `${typeof controllerName}:checkDataDeletionTaskStatus`;
handler: MetaMetricsDataDeletionController['checkDataDeletionTaskStatus'];
export type UpdateDataDeletionTaskStatusAction = {
type: `${typeof controllerName}:updateDataDeletionTaskStatus`;
handler: MetaMetricsDataDeletionController['updateDataDeletionTaskStatus'];
};

// Union of all possible actions for the messenger
export type MetaMetricsDataDeletionControllerMessengerActions =
| CreateMetaMetricsDataDeletionTaskAction
| CheckDataDeletionTaskStatusAction;
| UpdateDataDeletionTaskStatusAction;

// Type for the messenger of AccountOrderController
export type MetaMetricsDataDeletionControllerMessenger =
Expand All @@ -95,24 +108,24 @@ export default class MetaMetricsDataDeletionController extends BaseController<
MetaMetricsDataDeletionState,
MetaMetricsDataDeletionControllerMessenger
> {
private metaMetricsController: MetaMetricsController;
private metaMetricsId;

/**
* Creates a MetaMetricsDataDeletionController instance.
*
* @param args - The arguments to this function.
* @param args.messenger - Messenger used to communicate with BaseV2 controller.
* @param args.state - Initial state to set on this controller.
* @param args.metaMetricsController
* @param args.metaMetricsStore
*/
constructor({
messenger,
state,
metaMetricsController,
metaMetricsStore,
}: {
messenger: MetaMetricsDataDeletionControllerMessenger;
state?: MetaMetricsDataDeletionState;
metaMetricsController: MetaMetricsController;
metaMetricsStore: ObservableStore<MetaMetricsControllerState>;
}) {
// Call the constructor of BaseControllerV2
super({
Expand All @@ -121,7 +134,7 @@ export default class MetaMetricsDataDeletionController extends BaseController<
name: controllerName,
state: { ...defaultState, ...state },
});
this.metaMetricsController = metaMetricsController;
this.metaMetricsId = metaMetricsStore.getState().metaMetricsId;
}

_formatDeletionDate() {
Expand All @@ -134,49 +147,41 @@ export default class MetaMetricsDataDeletionController extends BaseController<
return `${day}/${month}/${year}`;
}

/**
* Creating the delete regulation using source regulation
*
*/
async createMetaMetricsDataDeletionTask(): Promise<void> {
const { metaMetricsId } = this.metaMetricsController.store.getState();
if (!metaMetricsId) {
if (!this.metaMetricsId) {
throw new Error('MetaMetrics ID not found');
}

try {
const { data } = await createDataDeletionRegulationTask(
metaMetricsId as string,
);
this.update((state) => {
state.metaMetricsDataDeletionId = data?.regulateId as string;
state.metaMetricsDataDeletionDate = this._formatDeletionDate();
return state;
});
await this.checkDataDeletionTaskStatus();
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error ?? '');
throw new Error(`Metametrics Data Deletion Error: ${errorMessage}`);
}
const { data } = await createDataDeletionRegulationTask(this.metaMetricsId);
this.update((state) => {
state.metaMetricsDataDeletionId = data?.regulateId;
state.metaMetricsDataDeletionDate = this._formatDeletionDate();
return state;
});
await this.updateDataDeletionTaskStatus();
}

async checkDataDeletionTaskStatus(): Promise<void> {
/**
* To check eth status of the current delete regulation.
*/
async updateDataDeletionTaskStatus(): Promise<void> {
const deleteRegulationId = this.state.metaMetricsDataDeletionId;
if (deleteRegulationId.length === 0) {
throw new Error('Delete Regulation id not found');
}

try {
const { data } = await fetchDeletionRegulationStatus(deleteRegulationId);
const { data } = await fetchDeletionRegulationStatus(deleteRegulationId);

const regulation = data?.regulation as Record<string, string>;
this.update((state) => {
state.metaMetricsDataDeletionStatus =
regulation.overallStatus as DeleteRegulationStatus;
const regulation = data?.regulation;
this.update((state) => {
state.metaMetricsDataDeletionStatus =
regulation.overallStatus as DeleteRegulationStatus;

return state;
});
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error ?? '');
throw new Error(`Metametrics Data Deletion Error: ${errorMessage}`);
}
return state;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getFetchWithTimeout from '../../../../../shared/modules/fetch-with-timeout';
import { DeleteRegulationAPIResponse } from '../metametrics-data-deletion';
import { CurrentRegulationStatus, RegulationId } from '../types';

const analyticsDataDeletionSourceId =
process.env.ANALYTICS_DATA_DELETION_SOURCE_ID;
Expand All @@ -10,7 +10,7 @@ const fetchWithTimeout = getFetchWithTimeout();

export async function createDataDeletionRegulationTask(
metaMetricsId: string,
): Promise<DeleteRegulationAPIResponse> {
): Promise<RegulationId> {
if (!analyticsDataDeletionEndpoint || !analyticsDataDeletionSourceId) {
throw new Error('Segment API source ID or endpoint not found');
}
Expand All @@ -32,7 +32,7 @@ export async function createDataDeletionRegulationTask(

export async function fetchDeletionRegulationStatus(
deleteRegulationId: string,
): Promise<DeleteRegulationAPIResponse> {
): Promise<CurrentRegulationStatus> {
if (!analyticsDataDeletionEndpoint) {
throw new Error('Segment API source ID or endpoint not found');
}
Expand Down
13 changes: 13 additions & 0 deletions app/scripts/controllers/metametrics-data-deletion/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @type RegulationId
* Response from API after the delete regulation creation.
*/
export type RegulationId = { data: Record<string, string> };

/**
* @type CurrentRegulationStatus
* Response from API after the status check of current delete regulation.
*/
export type CurrentRegulationStatus = {
data: Record<string, Record<string, string>>;
};
2 changes: 1 addition & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ export default class MetamaskController extends EventEmitter {
new MetaMetricsDataDeletionController({
messenger: metaMetricsDataDeletionMessenger,
state: initState.metaMetricsDataDeletionController,
metaMetricsController: this.metaMetricsController,
metaMetricsStore: this.metaMetricsController.store,
});

const gasFeeMessenger = this.controllerMessenger.getRestricted({
Expand Down
4 changes: 2 additions & 2 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5104,10 +5104,10 @@ export function createMetaMetricsDataDeletionTask(): ThunkAction<
/**
* To check the status of the current delete regulation.
*/
export async function checkDataDeletionTaskStatus(): Promise<void> {
export async function UpdateDataDeletionTaskStatusAction(): Promise<void> {
return async () => {
await submitRequestToBackground<DataDeletionResponse>(
'checkDataDeletionTaskStatus',
'UpdateDataDeletionTaskStatusAction',
);
};
}
Expand Down

0 comments on commit 4c9ece3

Please sign in to comment.