Skip to content

Commit

Permalink
feat: reports api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Dec 24, 2024
1 parent c25e89c commit 8f17409
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 21 deletions.
171 changes: 150 additions & 21 deletions src/reports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class Reports extends CrowdinApi {
*/
addReportSettingsTemplate(
projectId: number,
request: Omit<ReportsModel.ReportSettings, 'id' | 'createdAt' | 'updatedAt'>,
request: ReportsModel.AddReportSettingsRequest,
): Promise<ResponseObject<ReportsModel.ReportSettings>> {
const url = `${this.url}/projects/${projectId}/reports/settings-templates`;
return this.post(url, request, this.defaultConfig());
Expand Down Expand Up @@ -327,12 +327,76 @@ export class Reports extends CrowdinApi {
const url = `${this.url}/projects/${projectId}/reports/settings-templates/${reportSettingsTemplateId}`;
return this.delete(url, this.defaultConfig());
}

/**
* @param userId user identifier
* @param options optional parameters for the request
* @see https://support.crowdin.com/developer/api/v2/#tag/Reports/operation/api.users.reports.settings-templates.getMany
*/
listUserReportSettingsTemplates(
userId: number,
options?: PaginationOptions,
): Promise<ResponseList<ReportsModel.UserReportSettings>> {
const url = `${this.url}/users/${userId}/reports/settings-templates`;
return this.getList(url, options?.limit, options?.offset);
}

/**
* @param userId user identifier
* @param request request body
* @see https://support.crowdin.com/developer/api/v2/#tag/Reports/operation/api.users.reports.settings-templates.post
*/
addUserReportSettingsTemplate(
userId: number,
request: ReportsModel.AddUserReportSettingsRequest,
): Promise<ResponseObject<ReportsModel.UserReportSettings>> {
const url = `${this.url}/users/${userId}/reports/settings-templates`;
return this.post(url, request, this.defaultConfig());
}

/**
* @param userId user identifier
* @param reportSettingsTemplateId report settings template identifier
* @see https://support.crowdin.com/developer/api/v2/#tag/Reports/operation/api.users.reports.settings-templates.get
*/
getUserReportSettingsTemplate(
userId: number,
reportSettingsTemplateId: number,
): Promise<ResponseObject<ReportsModel.UserReportSettings>> {
const url = `${this.url}/users/${userId}/reports/settings-templates/${reportSettingsTemplateId}`;
return this.get(url, this.defaultConfig());
}

/**
* @param userId user identifier
* @param reportSettingsTemplateId report settings template identifier
* @param request request body
* @see https://support.crowdin.com/developer/api/v2/#tag/Reports/operation/api.users.reports.settings-templates.patch
*/
editUserReportSettingsTemplate(
userId: number,
reportSettingsTemplateId: number,
request: PatchRequest[],
): Promise<ResponseObject<ReportsModel.UserReportSettings>> {
const url = `${this.url}/users/${userId}/reports/settings-templates/${reportSettingsTemplateId}`;
return this.patch(url, request, this.defaultConfig());
}

/**
* @param userId user identifier
* @param reportSettingsTemplateId report settings template identifier
* @see https://support.crowdin.com/developer/api/v2/#tag/Reports/operation/api.users.reports.settings-templates.delete
*/
deleteUserReportSettingsTemplate(userId: number, reportSettingsTemplateId: number): Promise<void> {
const url = `${this.url}/users/${userId}/reports/settings-templates/${reportSettingsTemplateId}`;
return this.delete(url, this.defaultConfig());
}
}

export namespace ReportsModel {
export interface ReportArchive {
id: number;
scopeType: number;
scopeType: string;
scopeId: number;
userId: number;
name: string;
Expand Down Expand Up @@ -375,6 +439,7 @@ export namespace ReportsModel {
individualRates: IndividualRate[];
netRateSchemes: NetRateSchemas;
excludeApprovalsForEditedTranslations?: boolean;
preTranslatedStringsCategorizationAdjustment?: boolean;
groupBy?: GroupBy;
dateFrom?: string;
dateTo?: string;
Expand Down Expand Up @@ -439,32 +504,78 @@ export namespace ReportsModel {
dateTo?: string;
}

export type ReportSchema =
| CostEstimationPostEndingSchema
| CostEstimationPostEndingSchemaByTask
| TranslationCostsPostEndingSchema
| TranslationCostsPostEndingSchemaByTask
| TopMembersSchema
| ContributionRawDataSchema
| PreTranslateEfficiencySchema;
export type GenerateReportRequest =
| PreTranslateAccuracy
| TranslateAccuracy
| CostEstimationPostEnding
| TranslationCostsPostEnding
| TopMembers
| ContributionRawData;

export interface GenerateReportRequest {
name:
| 'top-members'
| 'contribution-raw-data'
| 'costs-estimation-pe'
| 'translation-costs-pe'
| 'pre-translate-efficiency';
schema: ReportSchema;
export type ReportSchema = Pick<GenerateReportRequest, 'schema'>;

export interface PreTranslateAccuracy {
name: 'pre-translate-efficiency' | 'pre-translate-accuracy';
schema: PreTranslateAccuracySchema | PreTranslateAccuracySchemaByTask;
}

export interface TranslateAccuracy {
name: 'translator-accuracy';
schema: TranslateAccuracySchema;
}

export interface CostEstimationPostEnding {
name: 'costs-estimation-pe';
schema: CostEstimationPostEndingSchema | CostEstimationPostEndingSchemaByTask;
}

export interface TranslationCostsPostEnding {
name: 'translation-costs-pe';
schema: TranslationCostsPostEndingSchema | TranslationCostsPostEndingSchemaByTask;
}

export interface TopMembers {
name: 'top-members';
schema: TopMembersSchema;
}

export interface ContributionRawData {
name: 'contribution-raw-data';
schema: ContributionRawDataSchema | ContributionRawDataSchemaByTask;
}

export interface ReportStatusAttributes<S> {
format: Format;
reportName: string;
projectIds?: number[];
schema: S;
}

export interface PreTranslateAccuracySchema {
unit?: Unit;
format?: Format;
postEditingCategories?: string[];
languageId?: string;
dateFrom?: string;
dateTo?: string;
}

export interface PreTranslateAccuracySchemaByTask {
unit?: Unit;
format?: Format;
postEditingCategories?: string[];
taskId?: number;
}

export interface TranslateAccuracySchema {
unit?: Unit;
format?: Format;
postEditingCategories?: string[];
languageId?: string;
userIds?: number[];
dateFrom?: string;
dateTo?: string;
}

export interface CostEstimationPostEndingSchema {
unit?: Unit;
currency?: Currency;
Expand Down Expand Up @@ -506,6 +617,7 @@ export namespace ReportsModel {
netRateSchemes: NetRateSchemas;
taskId?: number;
excludeApprovalsForEditedTranslations?: boolean;
preTranslatedStringsCategorizationAdjustment?: boolean;
}

export interface TranslationCostsPostEndingSchema {
Expand Down Expand Up @@ -582,10 +694,23 @@ export namespace ReportsModel {
unit: Unit;
config: ReportSettinsConfig;
isPublic: boolean;
isGlobal: boolean;
createdAt: string;
updatedAt: string;
}

export interface AddReportSettingsRequest {
name: string;
currency: Currency;
unit: Unit;
config: ReportSettinsConfig;
isPublic?: boolean;
isGlobal?: boolean;
}

export type UserReportSettings = Omit<ReportSettings, 'isPublic' | 'isGlobal'>;
export type AddUserReportSettingsRequest = Omit<AddReportSettingsRequest, 'isPublic' | 'isGlobal'>;

export interface ReportSettinsConfig {
baseRates: BaseRate;
netRateSchemes: NetRateSchemas[];
Expand Down Expand Up @@ -637,11 +762,15 @@ export namespace ReportsModel {
matchType: Mode;
price: number;
}[];
mtMatch?: {
mtMatch: {
matchType: Mode;
price: number;
}[];
suggestionMatch: {
matchType: Mode;
price: number;
}[];
suggestionMatch?: {
aiMatch?: {
matchType: Mode;
price: number;
}[];
Expand Down
110 changes: 110 additions & 0 deletions tests/reports/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,79 @@ describe('Reports API', () => {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200)
.get(`/users/${userId}/reports/settings-templates`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200, {
data: [
{
data: {
id: reportSettingsTemplateId,
},
},
],
pagination: {
offset: 0,
limit: 1,
},
})
.post(
`/users/${userId}/reports/settings-templates`,
{
name: reportName,
currency,
unit,
config,
},
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200, {
data: {
id: reportSettingsTemplateId,
},
})
.get(`/users/${userId}/reports/settings-templates/${reportSettingsTemplateId}`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200, {
data: {
id: reportSettingsTemplateId,
},
})
.patch(
`/users/${userId}/reports/settings-templates/${reportSettingsTemplateId}`,
[
{
value: reportName,
op: 'replace',
path: '/name',
},
],
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200, {
data: {
id: reportSettingsTemplateId,
},
})
.delete(`/users/${userId}/reports/settings-templates/${reportSettingsTemplateId}`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200);
});

Expand Down Expand Up @@ -517,4 +590,41 @@ describe('Reports API', () => {
it('Delete Report Settings Template', async () => {
await api.deleteReportSettingsTemplate(projectId, reportSettingsTemplateId);
});

it('List User Report Settings Templates', async () => {
const templates = await api.listUserReportSettingsTemplates(userId);
expect(templates.data.length).toBe(1);
expect(templates.data[0].data.id).toBe(reportSettingsTemplateId);
expect(templates.pagination.limit).toBe(1);
});

it('Add User Report Settings Template', async () => {
const template = await api.addUserReportSettingsTemplate(userId, {
config,
currency,
name: reportName,
unit,
});
expect(template.data.id).toBe(reportSettingsTemplateId);
});

it('Get User Report Settings Template', async () => {
const template = await api.getUserReportSettingsTemplate(userId, reportSettingsTemplateId);
expect(template.data.id).toBe(reportSettingsTemplateId);
});

it('Edit User Report Settings Template', async () => {
const template = await api.editUserReportSettingsTemplate(userId, reportSettingsTemplateId, [
{
op: 'replace',
path: '/name',
value: reportName,
},
]);
expect(template.data.id).toBe(reportSettingsTemplateId);
});

it('Delete User Report Settings Template', async () => {
await api.deleteUserReportSettingsTemplate(userId, reportSettingsTemplateId);
});
});

0 comments on commit 8f17409

Please sign in to comment.