Skip to content

Commit

Permalink
Merge pull request #7496 from ever-co/fix/ever-co/ever-gauzy-desktop/#44
Browse files Browse the repository at this point in the history
-automatic-report

[Fix] Ever-co/ever gauzy desktop#44 automatic report
  • Loading branch information
evereq authored Feb 3, 2024
2 parents fdd341d + d1c8b77 commit c660c1e
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 97 deletions.
92 changes: 35 additions & 57 deletions packages/desktop-libs/src/lib/offline/dao/interval.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@ export class IntervalDAO implements DAO<IntervalTO> {
this._trx = new IntervalTransaction(this._provider);
}

private _isJSON(value: any): boolean {
try {
JSON.parse(value);
return true;
} catch (error) {
return false;
}
}

public async findAll(): Promise<IntervalTO[]> {
try {
return await this._provider.connection<IntervalTO>(
TABLE_NAME_INTERVALS
);
return await this._provider.connection<IntervalTO>(TABLE_NAME_INTERVALS);
} catch (error) {
console.log('[dao]: ', 'interval backed up fails : ', error);
return [];
}
}

public async findAllSynced(
isSynced: boolean,
user: UserTO
): Promise<IntervalTO[]> {
public async findAllSynced(isSynced: boolean, user: UserTO): Promise<IntervalTO[]> {
try {
return await this._provider
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.where('employeeId', user.employeeId)
.andWhere('isDeleted', false)
.andWhere('synced', isSynced);
} catch (error) {
console.log('[dao]: ', 'interval backed up fails : ', error);
Expand All @@ -46,7 +51,8 @@ export class IntervalDAO implements DAO<IntervalTO> {
try {
return await this._provider
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.where('id', id)[0];
.where('id', id)
.andWhere('isDeleted', false)[0];
} catch (error) {
console.log('[dao]: ', 'fail on find interval : ', error);
}
Expand All @@ -62,20 +68,13 @@ export class IntervalDAO implements DAO<IntervalTO> {

public async delete(value: Partial<IntervalTO>): Promise<void> {
try {
return await this._provider
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.where('id', value.id)
.del();
return await this._provider.connection<IntervalTO>(TABLE_NAME_INTERVALS).where('id', value.id).del();
} catch (error) {
console.log('[dao]: ', 'interval deleted fails : ', error);
}
}

public async backedUpNoSynced(
startedAt: Date,
stoppedAt: Date,
user: UserTO
): Promise<IntervalTO[]> {
public async backedUpNoSynced(startedAt: Date, stoppedAt: Date, user: UserTO): Promise<IntervalTO[]> {
try {
return await this._provider
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
Expand All @@ -85,6 +84,7 @@ export class IntervalDAO implements DAO<IntervalTO> {
qb
.whereBetween('startedAt', [new Date(startedAt), new Date(stoppedAt)])
.andWhere('synced', false)
.andWhere('isDeleted', false)
);
} catch (error) {
console.log('[dao]: ', 'interval backup fails : ', error);
Expand All @@ -105,6 +105,7 @@ export class IntervalDAO implements DAO<IntervalTO> {
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.count('* as total')
.where('employeeId', user.employeeId)
.where('isDeleted', false)
.andWhere('synced', isSynced);
} catch (error) {
console.log('[dao]: ', 'interval backed up fails : ', error);
Expand All @@ -127,35 +128,21 @@ export class IntervalDAO implements DAO<IntervalTO> {
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.select('id', 'screenshots', 'startedAt as recordedAt')
.where('employeeId', user.employeeId)
.andWhere((qb) =>
qb
.whereNot('screenshots', '[]')
.orWhereRaw(query(this._providerFactory.dialect))
)
.where('isDeleted', false)
.andWhere((qb) => qb.whereNot('screenshots', '[]').orWhereRaw(query(this._providerFactory.dialect)))
.orderBy('id', 'desc')
.limit(10);
return latests.map((latest) => {
return {
...latest,
screenshots: this._isJSON(latest.screenshots)
? JSON.parse(latest.screenshots)
: latest.screenshots,
screenshots: this._isJSON(latest.screenshots) ? JSON.parse(latest.screenshots) : latest.screenshots
};
});
} catch (error) {
console.error('[SCREENSHOT_DAO_ERROR]', error);
}
}

private _isJSON(value: any): boolean {
try {
JSON.parse(value);
return true;
} catch (error) {
return false;
}
}

/**
* It deletes all the intervals that are not synced and then returns the remoteIds of the intervals
* that are synced
Expand Down Expand Up @@ -186,51 +173,42 @@ export class IntervalDAO implements DAO<IntervalTO> {
}

/**
* It deletes all the intervals that are not synced and that are between the given dates
* It soft deletes all the intervals that are not synced and that are between the given dates
* @param {Date} startedAt - Date,
* @param {Date} stoppedAt - Date - the date when the user stopped working
* @param {UserTO} user - UserTO
*/
public async deleteLocallyIdlesTime(
startedAt: Date,
stoppedAt: Date,
user: UserTO
): Promise<void> {
public async deleteLocallyIdlesTime(startedAt: Date, stoppedAt: Date, user: UserTO): Promise<void> {
try {
const subQuery = await this._provider
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.select('id')
.where('employeeId', user.employeeId)
.where((qb) =>
qb.whereBetween('stoppedAt', [new Date(startedAt), new Date(stoppedAt)])
);
await this._provider
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.whereIn(
'id',
subQuery.map(({ id }) => id)
)
.del();
.where('employeeId', user.employeeId)
.whereBetween('stoppedAt', [new Date(startedAt), new Date(stoppedAt)])
.update({ isDeleted: true });
} catch (error) {
console.log('[dao]: ', error);
}
}

public async deleteByRemoteId(remoteId: string): Promise<void> {
try {
return await this._provider.connection<IntervalTO>(TABLE_NAME_INTERVALS).where('remoteId', remoteId).del();
return await this._provider
.connection<IntervalTO>(TABLE_NAME_INTERVALS)
.where('remoteId', remoteId)
.update({ isDeleted: true });
} catch (error) {
console.log('[dao]: ', 'interval deleted fails : ', error);
}
}

public async lastSyncedInterval(employeeId: string): Promise<IntervalTO> {
const [interval] = await this._provider
return this._provider
.connection<TimerTO>(TABLE_NAME_INTERVALS)
.where('employeeId', employeeId)
.whereNotNull('remoteId')
.where((builder) => {
builder.whereNotNull('remoteId').andWhere('synced', true).andWhere('isDeleted', false);
})
.orderBy('id', 'desc')
.limit(1);
return interval;
.first();
}
}
5 changes: 2 additions & 3 deletions packages/desktop-libs/src/lib/offline/dao/timer.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class TimerDAO implements DAO<TimerTO> {
`${TABLE_NAME_INTERVALS}.timerId`
)
.where(`${TABLE_NAME_INTERVALS}.synced`, false)
.andWhere(`${TABLE_NAME_INTERVALS}.isDeleted`, false)
.andWhere(`${TABLE_NAME_TIMERS}.employeeId`, user.employeeId)
.andWhere((qb) =>
qb
Expand Down Expand Up @@ -140,7 +141,7 @@ export class TimerDAO implements DAO<TimerTO> {
.andWhere(`${TABLE_NAME_TIMERS}.synced`, true)
.orderBy(`${TABLE_NAME_TIMERS}.id`, 'asc');

const timersWithIntervals = timers.map((timer: TimerTO) => ({
return timers.map((timer: TimerTO) => ({
timer,
intervals: intervals
.map((interval: IntervalTO) => ({
Expand All @@ -152,8 +153,6 @@ export class TimerDAO implements DAO<TimerTO> {
(interval: IntervalTO) => interval.timerId === timer.id
),
}));

return timersWithIntervals
}

public async findFailedRuns(user: UserTO): Promise<ISequence[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Knex } from 'knex';
import { TABLE_NAME_INTERVALS } from '../../dto';

export async function up(knex: Knex): Promise<void> {
return knex.schema.alterTable(TABLE_NAME_INTERVALS, function (table) {
// Add a new boolean column named 'isDeleted' with default value false
table.boolean('isDeleted').defaultTo(false);
});
}

export async function down(knex: Knex): Promise<void> {
return knex.schema.alterTable(TABLE_NAME_INTERVALS, function (table) {
// Drop the 'isDeleted' column if the migration needs to be reverted
table.dropColumn('isDeleted');
});
}

1 change: 1 addition & 0 deletions packages/desktop-libs/src/lib/offline/dto/interval.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IntervalTO extends BaseTO {
stoppedAt: Date;
synced: boolean;
timerId: number;
isDeleted: boolean;
}

export const TABLE_NAME_INTERVALS = 'intervals';
12 changes: 11 additions & 1 deletion packages/desktop-libs/src/lib/offline/models/interval.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Serializable } from '../../interfaces';
import { IntervalTO } from '../dto/interval.dto';
import { IntervalTO } from '../dto';
import { Base } from './base.model';

export class Interval
Expand All @@ -19,6 +19,7 @@ export class Interval
private _stoppedAt: Date;
private _synced: boolean;
private _timerId: number;
private _isDeleted: boolean;

constructor(interval: IntervalTO) {
super(
Expand All @@ -41,6 +42,14 @@ export class Interval
this._synced = interval.synced;
}

public get isDeleted(): boolean {
return this._isDeleted;
}

public set isDeleted(value: boolean) {
this._isDeleted = value;
}

public get timerId(): number {
return this._timerId;
}
Expand Down Expand Up @@ -122,6 +131,7 @@ export class Interval
}
public toObject(): IntervalTO {
return {
isDeleted: this._isDeleted,
activities: this._activities,
screenshots: this._screenshots,
startedAt: this._startedAt,
Expand Down
Loading

0 comments on commit c660c1e

Please sign in to comment.