-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a new controller and service class for metametrics deletion
- Loading branch information
1 parent
cfcbd1e
commit 614ccfd
Showing
2 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
app/scripts/controllers/metametrics-data-deletion/metametrics-data-deletion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { ObservableStore } from '@metamask/obs-store'; | ||
import MetaMetricsController from '../metametrics'; | ||
import { | ||
createDataDeletionRegulationTask, | ||
fetchDeletionRegulationStatus, | ||
} from './services/services'; | ||
|
||
export type DataDeleteDate = string | undefined; | ||
export type DataDeleteRegulationId = string | undefined; | ||
|
||
export type RegulationId = Record<string, string>; | ||
export type CurrentRegulationStatus = Record<string, Record<string, string>>; | ||
export type DeleteRegulationAPIResponse = { | ||
data: Record<string, RegulationId | CurrentRegulationStatus>; | ||
}; | ||
|
||
export enum responseStatus { | ||
ok = 'ok', | ||
error = 'error', | ||
} | ||
|
||
export type DataDeletionResponseStatus = { | ||
status: responseStatus; | ||
metaMetricsDataDeletionStatus?: string | null; | ||
error?: string; | ||
}; | ||
|
||
export type MetaMetricsDataDeletionState = { | ||
metaMetricsDataDeletionId: DataDeleteRegulationId; | ||
metaMetricsDataDeletionDate: DataDeleteDate; | ||
}; | ||
|
||
const defaultState: MetaMetricsDataDeletionState = { | ||
metaMetricsDataDeletionId: undefined, | ||
metaMetricsDataDeletionDate: undefined, | ||
}; | ||
|
||
/** | ||
* Controller responsible for maintaining | ||
* state related to Metametrics data deletion | ||
*/ | ||
export default class MetaMetricsDataDeletionController { | ||
/** | ||
* Observable store containing controller data. | ||
*/ | ||
store: ObservableStore<MetaMetricsDataDeletionState>; | ||
|
||
private metaMetricsController: MetaMetricsController; | ||
|
||
/** | ||
* Constructs a DeleteMetaMetricsData controller. | ||
* | ||
* @param options - the controller options | ||
* @param options.initState - Initial controller state. | ||
* @param options.metaMetricsController | ||
*/ | ||
constructor({ | ||
initState, | ||
metaMetricsController, | ||
}: { | ||
initState: MetaMetricsDataDeletionState; | ||
metaMetricsController: MetaMetricsController; | ||
}) { | ||
this.store = new ObservableStore({ | ||
...defaultState, | ||
...initState, | ||
}); | ||
this.metaMetricsController = metaMetricsController; | ||
} | ||
|
||
_formatDeletionDate() { | ||
const currentDate = new Date(); | ||
const day = currentDate.getUTCDate(); | ||
const month = currentDate.getUTCMonth() + 1; | ||
const year = currentDate.getUTCFullYear(); | ||
|
||
// format the date in the format DD/MM/YYYY | ||
return `${day}/${month}/${year}`; | ||
} | ||
|
||
async createMetaMetricsDataDeletionTask(): Promise<DataDeletionResponseStatus> { | ||
const segmentSourceId = process.env.SEGMENT_DELETE_API_SOURCE_ID; | ||
const segmentRegulationEndpoint = process.env.SEGMENT_REGULATIONS_ENDPOINT; | ||
|
||
if (!segmentSourceId || !segmentRegulationEndpoint) { | ||
return { | ||
status: responseStatus.error, | ||
error: 'Segment API source ID or endpoint not found', | ||
}; | ||
} | ||
const { metaMetricsId } = this.metaMetricsController.store.getState(); | ||
|
||
try { | ||
const { data } = await createDataDeletionRegulationTask( | ||
metaMetricsId as string, | ||
); | ||
|
||
this.store.updateState({ | ||
metaMetricsDataDeletionId: data?.data?.regulateId as string, | ||
metaMetricsDataDeletionDate: this._formatDeletionDate(), | ||
}); | ||
|
||
return { status: responseStatus.ok }; | ||
} catch (error: unknown) { | ||
return { | ||
status: responseStatus.error, | ||
error: error as string, | ||
}; | ||
} | ||
} | ||
|
||
async checkDataDeletionTaskStatus(): Promise<DataDeletionResponseStatus> { | ||
const segmentRegulationEndpoint = process.env.SEGMENT_REGULATIONS_ENDPOINT; | ||
|
||
if (!segmentRegulationEndpoint) { | ||
return { | ||
status: responseStatus.error, | ||
error: 'Segment API source ID or endpoint not found', | ||
}; | ||
} | ||
try { | ||
const { data } = await fetchDeletionRegulationStatus( | ||
this.store.getState().metaMetricsDataDeletionId as string, | ||
); | ||
const regulation = data?.data?.regulation as Record<string, string>; | ||
|
||
return { | ||
status: responseStatus.ok, | ||
metaMetricsDataDeletionStatus: regulation.overallStatus, | ||
}; | ||
} catch (error: unknown) { | ||
return { | ||
status: responseStatus.error, | ||
error: error as string, | ||
}; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/scripts/controllers/metametrics-data-deletion/services/services.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import getFetchWithTimeout from '../../../../../shared/modules/fetch-with-timeout'; | ||
import { DeleteRegulationAPIResponse } from '../metametrics-data-deletion'; | ||
|
||
const fetchWithTimeout = getFetchWithTimeout(); | ||
|
||
export async function createDataDeletionRegulationTask( | ||
metaMetricsId: string, | ||
): Promise<DeleteRegulationAPIResponse> { | ||
const segmentSourceId = process.env.SEGMENT_DELETE_API_SOURCE_ID; | ||
const segmentRegulationEndpoint = process.env.SEGMENT_REGULATIONS_ENDPOINT; | ||
|
||
try { | ||
const response: unknown = await fetchWithTimeout( | ||
`${segmentRegulationEndpoint}/regulations/sources/${segmentSourceId}`, | ||
{ | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/vnd.segment.v1+json' }, | ||
body: JSON.stringify({ | ||
regulationType: 'DELETE_ONLY', | ||
subjectType: 'USER_ID', | ||
subjectIds: [metaMetricsId], | ||
}), | ||
}, | ||
); | ||
return response as DeleteRegulationAPIResponse; | ||
} catch (error: unknown) { | ||
throw new Error('Analytics Deletion Task Error'); | ||
} | ||
} | ||
|
||
export async function fetchDeletionRegulationStatus( | ||
deleteRegulationId: string, | ||
): Promise<DeleteRegulationAPIResponse> { | ||
const segmentRegulationEndpoint = process.env.SEGMENT_REGULATIONS_ENDPOINT; | ||
|
||
try { | ||
const response: unknown = await fetchWithTimeout( | ||
`${segmentRegulationEndpoint}/regulations/${deleteRegulationId}`, | ||
{ | ||
method: 'GET', | ||
headers: { 'Content-Type': 'application/vnd.segment.v1+json' }, | ||
}, | ||
); | ||
return response as DeleteRegulationAPIResponse; | ||
} catch (error: unknown) { | ||
throw new Error('Analytics Deletion Task Error'); | ||
} | ||
} |