Skip to content

Commit

Permalink
fix(frontend): refactor report ability to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Feb 19, 2025
1 parent 680117d commit 24c842a
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 205 deletions.
62 changes: 44 additions & 18 deletions frontend/app/abilities/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,49 @@ export default class ReportAbility extends Ability {
return this.currentUser.user;
}

get canEdit() {
const isEditable =
this.user?.isSuperuser ||
(!this.model?.verifiedBy?.get("id") &&
(this.model?.user?.get("id") === this.user?.get("id") ||
(this.model?.user?.get("supervisors") ?? [])
.map((s) => s.id)
.includes(this.user?.get("id"))));
const isReviewer =
(this.model?.taskAssignees ?? [])
.concat(
this.model?.projectAssignees ?? [],
this.model?.customerAssignees ?? [],
)
.filter((a) => a?.user)
.map((a) => a.user.get("id"))
.includes(this.user?.get("id")) && !this.model?.verifiedBy?.get("id");
return isEditable || isReviewer;
get canEditSync() {
if (this.user?.isSuperuser) {
return true;
}

if (this.model?.verifiedBy?.get("id")) {
return false;
}

if (this.model?.user?.get("id") === this.user?.get("id")) {
return true;
}

return false;
}

async isReviewer() {
return ((await this.model?.taskAssignees) ?? [])
.concat(
(await this.model?.projectAssignees) ?? [],
(await this.model?.customerAssignees) ?? [],
)
.filter((a) => a?.user)
.map((a) => a.user.get("id"))
.includes(this.user?.get("id"));
}

async isSupervisee() {
return ((await this.model?.user?.get("supervisors")) ?? [])
.map((s) => s.id)
.includes(this.user?.get("id"));
}

async canEditAsync() {
if (this.model?.verifiedBy?.get("id")) {
return false;
}

const isSupervisee = await this.isSupervisee();
if (isSupervisee) {
return true;
}

return await this.isReviewer();
}
}
24 changes: 10 additions & 14 deletions frontend/app/analysis/index/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ export default class AnalysisController extends QPController {
return `The export limit is ${this.exportLimit}. Please use filters to reduce the amount of reports.`;
}

get canBill() {
return (
this.currentUser.user.isAccountant || this.currentUser.user.isSuperuser
);
get isAccountant() {
return this.currentUser.user.isAccountant;
}

get appliedFilters() {
Expand Down Expand Up @@ -362,16 +360,14 @@ export default class AnalysisController extends QPController {

@action
selectRow(report) {
if (this.abilities.can("edit report", report) || this.canBill) {
const selected = this.selectedReportIds;

if (selected.includes(report.id)) {
this.selectedReportIds = A([
...selected.filter((id) => id !== report.id),
]);
} else {
this.selectedReportIds = A([...selected, report.id]);
}
const selected = this.selectedReportIds;

if (selected.includes(report.id)) {
this.selectedReportIds = A([
...selected.filter((id) => id !== report.id),
]);
} else {
this.selectedReportIds = A([...selected, report.id]);
}
}
}
106 changes: 57 additions & 49 deletions frontend/app/analysis/index/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -330,56 +330,64 @@
<tbody>
{{#each reports as |report|}}
{{! template-lint-disable}}
<Table::Tr
class="{{if
(includes report.id this.selectedReportIds)
'selected bg-primary text-foreground-primary'
'striped'
<CanEdit @report={{report}} as |canEdit|>
<Table::Tr
class="{{if
(includes report.id this.selectedReportIds)
'selected bg-primary text-foreground-primary'
'striped'
}}
{{if
(or this.isAccountant canEdit)
'text-foreground-accent cursor-pointer'
}}
shadow-foreground/5 align-top shadow transition-colors"
{{on
"click"
(conditional-action
(or this.isAccountant canEdit)
(fn this.selectRow report)
)
}}
{{if
(or this.canBill (can 'edit report' report))
'text-foreground-accent cursor-pointer'
}}
shadow-foreground/5 align-top shadow transition-colors"
{{on "click" (fn this.selectRow report)}}
>
<Table::Td>{{report.user.username}}</Table::Td>
<Table::Td>{{moment-format
report.date
"DD.MM.YYYY"
}}</Table::Td>
<Table::Td>{{format-duration
report.duration
false
}}</Table::Td>
<Table::Td
>{{report.task.project.customer.name}}</Table::Td>
<Table::Td>{{report.task.project.name}}</Table::Td>
<Table::Td>{{report.task.name}}</Table::Td>
<Table::Td><span
title="{{report.comment}}"
>{{report.comment}}</span></Table::Td>
<Table::Td>{{if
report.verifiedBy
report.verifiedBy.username
}}</Table::Td>
<Table::Td><Checkmark
@checked={{report.rejected}}
@highlight={{true}}
/></Table::Td>
<Table::Td><Checkmark
@checked={{report.review}}
@highlight={{true}}
/></Table::Td>
<Table::Td><Checkmark
@checked={{report.notBillable}}
@highlight={{true}}
/></Table::Td>
<Table::Td><Checkmark
@checked={{report.billed}}
@highlight={{true}}
/></Table::Td>
</Table::Tr>
>
<Table::Td>{{report.user.username}}</Table::Td>
<Table::Td>{{moment-format
report.date
"DD.MM.YYYY"
}}</Table::Td>
<Table::Td>{{format-duration
report.duration
false
}}</Table::Td>
<Table::Td
>{{report.task.project.customer.name}}</Table::Td>
<Table::Td>{{report.task.project.name}}</Table::Td>
<Table::Td>{{report.task.name}}</Table::Td>
<Table::Td><span
title="{{report.comment}}"
>{{report.comment}}</span></Table::Td>
<Table::Td>{{if
report.verifiedBy
report.verifiedBy.username
}}</Table::Td>
<Table::Td><Checkmark
@checked={{report.rejected}}
@highlight={{true}}
/></Table::Td>
<Table::Td><Checkmark
@checked={{report.review}}
@highlight={{true}}
/></Table::Td>
<Table::Td><Checkmark
@checked={{report.notBillable}}
@highlight={{true}}
/></Table::Td>
<Table::Td><Checkmark
@checked={{report.billed}}
@highlight={{true}}
/></Table::Td>
</Table::Tr>
</CanEdit>
{{/each}}
{{#if this._canLoadMore}}
<tr
Expand Down
9 changes: 9 additions & 0 deletions frontend/app/components/can-edit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{#let (can "editSync report" @report) as |sync|}}
{{#if sync}}
{{yield true}}
{{else}}
{{#let (can "editAsync report" @report) as |promise|}}
{{yield (await promise)}}
{{/let}}
{{/if}}
{{/let}}
Loading

0 comments on commit 24c842a

Please sign in to comment.