diff --git a/src/reports/index.ts b/src/reports/index.ts index f5e49b950..6d4efcb68 100644 --- a/src/reports/index.ts +++ b/src/reports/index.ts @@ -197,6 +197,63 @@ export class Reports extends CrowdinApi { return this.get(url, this.defaultConfig()); } + /** + * @param options optional parameters for the request + * @see https://support.crowdin.com/developer/enterprise/api/v2/#tag/Reports/operation/api.reports.settings-templates.getMany + */ + listOrganizationReportSettingsTemplates( + options?: ReportsModel.ListOrganizationReportSettingsParams, + ): Promise> { + let url = `${this.url}/reports/settings-templates`; + url = this.addQueryParam(url, 'projectId', options?.projectId); + url = this.addQueryParam(url, 'groupId', options?.groupId); + return this.getList(url, options?.limit, options?.offset); + } + + /** + * @param request request body + * @see https://support.crowdin.com/developer/enterprise/api/v2/#tag/Reports/operation/api.reports.settings-templates.post + */ + addOrganizationReportSettingsTemplate( + request: ReportsModel.AddOrganizationReportSettingsRequest, + ): Promise> { + const url = `${this.url}/reports/settings-templates`; + return this.post(url, request, this.defaultConfig()); + } + + /** + * @param reportSettingsTemplateId report settings template identifier + * @see https://support.crowdin.com/developer/enterprise/api/v2/#tag/Reports/operation/api.reports.settings-templates.get + */ + getOrganizationReportSettingsTemplate( + reportSettingsTemplateId: number, + ): Promise> { + const url = `${this.url}/reports/settings-templates/${reportSettingsTemplateId}`; + return this.get(url, this.defaultConfig()); + } + + /** + * @param reportSettingsTemplateId report settings template identifier + * @param request request body + * @see https://support.crowdin.com/developer/enterprise/api/v2/#tag/Reports/operation/api.reports.settings-templates.patch + */ + editOrganizationReportSettingsTemplate( + reportSettingsTemplateId: number, + request: PatchRequest[], + ): Promise> { + const url = `${this.url}/reports/settings-templates/${reportSettingsTemplateId}`; + return this.patch(url, request, this.defaultConfig()); + } + + /** + * @param reportSettingsTemplateId report settings template identifier + * @see https://support.crowdin.com/developer/enterprise/api/v2/#tag/Reports/operation/api.reports.settings-templates.delete + */ + deleteOrganizationReportSettingsTemplate(reportSettingsTemplateId: number): Promise { + const url = `${this.url}/reports/settings-templates/${reportSettingsTemplateId}`; + return this.delete(url, this.defaultConfig()); + } + /** * @param request request body * @see https://support.crowdin.com/enterprise/api/#operation/api.reports.post @@ -687,6 +744,11 @@ export namespace ReportsModel { dateTo?: string; } + export interface ListOrganizationReportSettingsParams extends PaginationOptions { + projectId?: number; + groupId?: number; + } + export interface ReportSettings { id: number; name: string; @@ -711,6 +773,15 @@ export namespace ReportsModel { export type UserReportSettings = Omit; export type AddUserReportSettingsRequest = Omit; + export type OrganizationReportSettings = Omit & { + projectId: number; + groupId: number; + }; + export type AddOrganizationReportSettingsRequest = Omit & { + projectId?: number; + groupId?: number; + }; + export interface ReportSettinsConfig { baseRates: BaseRate; netRateSchemes: NetRateSchemas[]; diff --git a/tests/reports/api.test.ts b/tests/reports/api.test.ts index 5895034eb..c6b993709 100644 --- a/tests/reports/api.test.ts +++ b/tests/reports/api.test.ts @@ -210,6 +210,79 @@ describe('Reports API', () => { url: downloadLink, }, }) + .get('/reports/settings-templates', undefined, { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }) + .reply(200, { + data: [ + { + data: { + id: reportSettingsTemplateId, + }, + }, + ], + pagination: { + offset: 0, + limit: 1, + }, + }) + .post( + '/reports/settings-templates', + { + name: reportName, + currency, + unit, + config, + }, + { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }, + ) + .reply(200, { + data: { + id: reportSettingsTemplateId, + }, + }) + .get(`/reports/settings-templates/${reportSettingsTemplateId}`, undefined, { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }) + .reply(200, { + data: { + id: reportSettingsTemplateId, + }, + }) + .patch( + `/reports/settings-templates/${reportSettingsTemplateId}`, + [ + { + value: reportName, + op: 'replace', + path: '/name', + }, + ], + { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }, + ) + .reply(200, { + data: { + id: reportSettingsTemplateId, + }, + }) + .delete(`/reports/settings-templates/${reportSettingsTemplateId}`, undefined, { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }) + .reply(200) .post( '/reports', { @@ -517,6 +590,43 @@ describe('Reports API', () => { expect(downloadUrl.data.url).toBe(downloadLink); }); + it('List Organization Report Settings Templates', async () => { + const templates = await api.listOrganizationReportSettingsTemplates(); + expect(templates.data.length).toBe(1); + expect(templates.data[0].data.id).toBe(reportSettingsTemplateId); + expect(templates.pagination.limit).toBe(1); + }); + + it('Add Organization Report Settings Template', async () => { + const template = await api.addOrganizationReportSettingsTemplate({ + config, + currency, + name: reportName, + unit, + }); + expect(template.data.id).toBe(reportSettingsTemplateId); + }); + + it('Get Organization Report Settings Template', async () => { + const template = await api.getOrganizationReportSettingsTemplate(reportSettingsTemplateId); + expect(template.data.id).toBe(reportSettingsTemplateId); + }); + + it('Edit Organization Report Settings Template', async () => { + const template = await api.editOrganizationReportSettingsTemplate(reportSettingsTemplateId, [ + { + op: 'replace', + path: '/name', + value: reportName, + }, + ]); + expect(template.data.id).toBe(reportSettingsTemplateId); + }); + + it('Delete Organization Report Settings Template', async () => { + await api.deleteOrganizationReportSettingsTemplate(reportSettingsTemplateId); + }); + it('Generate Organization Report', async () => { const report = await api.generateOrganizationReport({ name: reportName,