Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reports for upwork integration by employee #1698

Merged
merged 1 commit into from
Jul 13, 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
107 changes: 104 additions & 3 deletions apps/api/src/app/upwork/upwork.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Workdiary } from 'upwork-api/lib/routers/workdiary.js';
import { Snapshot } from 'upwork-api/lib/routers/snapshot.js';
import { Auth } from 'upwork-api/lib/routers/auth.js';
import { Users } from 'upwork-api/lib/routers/organization/users.js';
import { Time } from 'upwork-api/lib/routers/reports/time.js';
import { IntegrationMapSyncEntityCommand } from '../integration-map/commands';
import {
TimesheetGetCommand,
Expand Down Expand Up @@ -480,6 +481,7 @@ export class UpworkService {
employeeId,
config,
entitiesToSync,
providerRefernceId,
providerId
}) {
const syncedContracts = await this.syncContracts({
Expand All @@ -490,7 +492,7 @@ export class UpworkService {

if (!employeeId) {
const employee = await this._getUpworkGauzyEmployee(
providerId,
providerRefernceId,
integrationId,
organizationId,
config
Expand All @@ -510,6 +512,16 @@ export class UpworkService {
employeeId,
entity.datePicker.selectedDate
);
case 'reports':
return await this.syncReports(
organizationId,
integrationId,
config,
employeeId,
providerRefernceId,
providerId,
entity.datePicker.selectedDate
);
default:
return;
}
Expand Down Expand Up @@ -710,14 +722,14 @@ export class UpworkService {
}

private async _getUpworkGauzyEmployee(
providerId,
providerRefernceId,
integrationId,
organizationId,
config
) {
let { record } = await this._integrationMapService.findOneOrFail({
where: {
sourceId: providerId,
sourceId: providerRefernceId,
entity: IntegrationEntity.EMPLOYEE
}
});
Expand Down Expand Up @@ -774,4 +786,93 @@ export class UpworkService {
entity: IntegrationEntity.EMPLOYEE
});
}

async syncReports(
organizationId,
integrationId,
config,
employeeId,
providerRefernceId,
providerId,
forDate
) {
let promises = [];
promises.push(this._getFreelanceLimitedReports(config, providerId));
promises.push(await this._getFreelanceFullReports(config, providerId));

const reports = await Promise.all(promises);
return reports;
}

private async _getFreelanceLimitedReports(
config: IUpworkApiConfig,
providerId
) {
const api = new UpworkApi(config);
const reports = new Time(api);

const freelancerId = providerId;

const startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD');

const select = `SELECT
worked_on,
company_name,
assignment_name,
assignment_team_id,
hours,
task,
memo
WHERE worked_on > '${startOfMonth}' AND
worked_on <= '${endOfMonth}'`;

return new Promise((resolve, reject) => {
api.setAccessToken(config.accessToken, config.accessSecret, () => {
reports.getByFreelancerLimited(
freelancerId,
{
tq: select
},
(err, data) => (err ? reject(err) : resolve(data))
);
});
});
}

private async _getFreelanceFullReports(
config: IUpworkApiConfig,
providerId
) {
const api = new UpworkApi(config);
const reports = new Time(api);

const freelancerId = providerId;

const startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD');

const select = `SELECT
worked_on,
company_name,
assignment_name,
assignment_team_id,
hours,
task,
memo
WHERE worked_on > '${startOfMonth}' AND
worked_on <= '${endOfMonth}'`;

return new Promise((resolve, reject) => {
api.setAccessToken(config.accessToken, config.accessSecret, () => {
reports.getByFreelancerFull(
freelancerId,
{
tq: select
},
(err, data) => (err ? reject(err) : resolve(data))
);
});
});
}
}
21 changes: 17 additions & 4 deletions apps/gauzy/src/app/@core/services/upwork-store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ const contractSettings = {
max: new Date(),
selectedDate: new Date()
}
},
{
name: 'Reports',
key: 'reports',
relatedTo: [],
sync: true,
datePicker: {
max: new Date(),
selectedDate: new Date()
}
}
],
onlyContracts: false
Expand Down Expand Up @@ -94,10 +104,12 @@ export class UpworkStoreService {

const integrationId = this._selectedIntegrationId$.getValue();
const { id: organizationId } = this._os.selectedOrganization;

//map contract provider to get authorize info
const { provider__reference: providerId } = contracts.find(
(contract: IEngagement) => true
);
const {
provider__reference: providerRefernceId,
provider__id: providerId
} = contracts.find((contract: IEngagement) => true);

return this._us.syncContractsRelatedData({
integrationId,
Expand All @@ -106,7 +118,8 @@ export class UpworkStoreService {
entitiesToSync,
config,
employeeId: this.employeeId,
providerId
providerId,
providerRefernceId
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="reports-container">
<div class="mb-3 mt-3">
<button nbButton status="primary">
<nb-icon class="mr-1" icon="edit-outline"></nb-icon
>{{ 'BUTTONS.MANAGE' | translate }}
</button>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
import { TranslationBaseComponent } from 'apps/gauzy/src/app/@shared/language-base/translation-base.component';
import { TranslateService } from '@ngx-translate/core';

@Component({
selector: 'ngx-reports',
templateUrl: './reports.component.html',
styleUrls: ['./reports.component.scss']
})
export class ReportsComponent extends TranslationBaseComponent
implements OnInit, OnDestroy {
private _ngDestroy$: Subject<void> = new Subject();

constructor(public translateService: TranslateService) {
super(translateService);
}

ngOnInit() {}

ngOnDestroy() {
this._ngDestroy$.next();
this._ngDestroy$.complete();
}
}
3 changes: 2 additions & 1 deletion apps/gauzy/src/app/pages/upwork/upwork-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UpworkComponent } from './components/upwork/upwork.component';
import { UpworkAuthorizeComponent } from './components/upwork-authorize/upwork-authorize.component';
import { TransactionsComponent } from './components/transactions/transactions.component';
import { ContractsComponent } from './components/contracts/contracts.component';
import { ReportsComponent } from './components/reports/reports.component';

const routes: Routes = [
{
Expand All @@ -25,7 +26,7 @@ const routes: Routes = [
},
{
path: 'reports',
component: TransactionsComponent
component: ReportsComponent
},
{
path: 'transactions',
Expand Down
4 changes: 3 additions & 1 deletion apps/gauzy/src/app/pages/upwork/upwork.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { TableComponentsModule } from '../../@shared/table-components/table-comp
import { ContractsComponent } from './components/contracts/contracts.component';
import { SyncDataSelectionComponent } from './components/sync-data-selection/sync-data-selection.component';
import { EmployeeSelectorsModule } from '../../@theme/components/header/selectors/employee/employee.module';
import { ReportsComponent } from './components/reports/reports.component';

export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
Expand All @@ -36,7 +37,8 @@ export function HttpLoaderFactory(http: HttpClient) {
UpworkAuthorizeComponent,
TransactionsComponent,
ContractsComponent,
SyncDataSelectionComponent
SyncDataSelectionComponent,
ReportsComponent
],
imports: [
CommonModule,
Expand Down