Skip to content

Commit

Permalink
[mgmt] use modified_at from deal version instead of deal #351 finally
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnuesslein committed Mar 21, 2022
1 parent 85d71c9 commit 72dbaca
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 29 deletions.
2 changes: 2 additions & 0 deletions apps/graphql/schema/deal.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ type DealVersion {
deal: Deal
created_at: DateTime
created_by: User
modified_at: DateTime
modified_by: User
object_id: Int
}

Expand Down
2 changes: 2 additions & 0 deletions apps/graphql/schema/investor.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type InvestorVersion {
investor: Investor
created_at: DateTime
created_by: User
modified_at: DateTime
modified_by: User
object_id: Int
}

Expand Down
46 changes: 46 additions & 0 deletions apps/landmatrix/migrations/0050_auto_20220304_1049.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 3.2.12 on 2022-03-04 09:49

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('landmatrix', '0049_auto_20220224_1826'),
]

operations = [
migrations.AddField(
model_name='dealversion',
name='modified_at',
field=models.DateTimeField(blank=True, null=True),
),
migrations.AddField(
model_name='dealversion',
name='modified_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='investorversion',
name='modified_at',
field=models.DateTimeField(blank=True, null=True),
),
migrations.AddField(
model_name='investorversion',
name='modified_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='dealversion',
name='created_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='investorversion',
name='created_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL),
),
]
31 changes: 31 additions & 0 deletions apps/landmatrix/migrations/0051_auto_20220321_2215.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 3.2.12 on 2022-03-21 21:15

from django.db import migrations


def forward_func(apps, schema_editor):
for dv in apps.get_model("landmatrix", "DealVersion").objects.all():
print(dv.serialized_data["modified_at"])
dv.modified_at = dv.serialized_data["modified_at"]
dv.modified_by_id = dv.serialized_data["modified_by"]
dv.save()

for iv in apps.get_model("landmatrix", "InvestorVersion").objects.all():
print(iv.serialized_data["modified_at"])
iv.modified_at = iv.serialized_data["modified_at"]
iv.modified_by_id = iv.serialized_data["modified_by"]
iv.save()

def backward_func(apps, schema_editor):
pass


class Migration(migrations.Migration):

dependencies = [
('landmatrix', '0050_auto_20220304_1049'),
]

operations = [
migrations.RunPython(forward_func, backward_func)
]
14 changes: 14 additions & 0 deletions apps/landmatrix/models/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class Version(models.Model):
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name="+",
)
modified_at = models.DateTimeField(null=True, blank=True)
modified_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name="+",
)
serialized_data = models.JSONField()

Expand All @@ -47,6 +56,11 @@ def __str__(self):
def save(self, *args, **kwargs):
if not self.pk and not self.created_at:
self.created_at = timezone.now()
# NOTE: This ought to be refactored already.
# It's not very nice to just strip the modified_at timestamp out of
# the serialized data :S
self.modified_at = self.serialized_data["modified_at"]
self.modified_by_id = self.serialized_data["modified_by"]
super().save(*args, **kwargs)

@classmethod
Expand Down
22 changes: 18 additions & 4 deletions frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ export function sortAnything<T extends { [key: string | number]: any }>(
sortAscending: boolean
): Array<T> {
function sortFunction(a: T, b: T) {
const fieldx = sortAscending ? a[sortField] : b[sortField];
const fieldy = sortAscending ? b[sortField] : a[sortField];
let fieldx;
let fieldy;
// NOTE: Nasty hack just to accomodate management.vue
if (sortField.startsWith("current_draft.")) {
const field = sortField.replace("current_draft.", "");
fieldx = sortAscending ? a.current_draft?.[field] : b.current_draft?.[field];
fieldy = sortAscending ? b.current_draft?.[field] : a.current_draft?.[field];
} else {
fieldx = sortAscending ? a[sortField] : b[sortField];
fieldy = sortAscending ? b[sortField] : a[sortField];
}

if (fieldx === null) return -1;
if (fieldy === null) return 1;
if (fieldx === null || fieldx === undefined) return -1;
if (fieldy === null || fieldy === undefined) return 1;

switch (typeof fieldx) {
case "number":
Expand All @@ -41,6 +50,11 @@ export function sortAnything<T extends { [key: string | number]: any }>(
case "object": {
if (Array.isArray(fieldx)) return fieldx.length - fieldy.length;

if ("username" in fieldx && "username" in fieldy)
return fieldy.username
.toLocaleLowerCase()
.localeCompare(fieldx.username.toLocaleLowerCase());

if ("name" in fieldx && "name" in fieldy)
return fieldy.name.localeCompare(fieldx.name);

Expand Down
90 changes: 65 additions & 25 deletions frontend/src/views/Manager/Management.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
>{{ $t("Created by") }}</span
>
</th>
<th :class="{ selected: sortField === 'modified_at', asc: sortAscending }">
<th>
<DatePicker
v-model="modified_daterange"
:max-date="new Date()"
Expand Down Expand Up @@ -182,14 +182,20 @@
track-by="id"
/>
<span
:class="{ selected: sortField === 'modified_at', asc: sortAscending }"
@click="setSort('modified_at')"
:class="{
selected: sortField === 'current_draft.modified_at',
asc: sortAscending,
}"
@click="setSort('current_draft.modified_at')"
>{{ $t("Last modified at") }}</span
>
/
<span
:class="{ selected: sortField === 'modified_by', asc: sortAscending }"
@click="setSort('modified_by')"
:class="{
selected: sortField === 'current_draft.modified_by',
asc: sortAscending,
}"
@click="setSort('current_draft.modified_by')"
>{{ $t("Last modified by") }}</span
>
</th>
Expand Down Expand Up @@ -272,7 +278,24 @@
:key="`${obj.id}-${fieldName}`"
:class="`field-${fieldName}`"
>
<div v-if="fieldName === 'current_draft.modified_at'">
<div>
{{
obj.current_draft && obj.current_draft.modified_at
? dayjs(obj.current_draft.modified_at).format("YYYY-MM-DD")
: ""
}}
</div>
<div>
{{
obj.current_draft && obj.current_draft.modified_by
? obj.current_draft.modified_by.username
: ""
}}
</div>
</div>
<DisplayField
v-else
:fieldname="fieldName"
:model="showDeals ? 'deal' : 'investor'"
:object-id="obj.id"
Expand All @@ -283,13 +306,13 @@
:value-classes="[]"
:wrapper-classes="[]"
/>
<div v-if="['created_at', 'modified_at'].includes(fieldName)">
<div v-if="fieldName === 'created_at'">
<DisplayField
:fieldname="fieldName.replace('_at', '_by')"
fieldname="created_by"
:model="showDeals ? 'deal' : 'investor'"
:object-id="obj.id"
:show-label="false"
:value="obj[fieldName.replace('_at', '_by')]"
:value="obj.created_by"
:value-classes="[]"
:wrapper-classes="[]"
/>
Expand Down Expand Up @@ -365,6 +388,7 @@
modified_by: null as User | null,
fully_updated_daterange: null as Date | null,
selected_combined_status: null as { id: string; name: string } | null,
dayjs,
};
},
apollo: {
Expand All @@ -385,6 +409,11 @@
current_draft {
id
created_at
modified_at
modified_by {
id
username
}
}
deal_size
created_at
Expand Down Expand Up @@ -442,6 +471,15 @@
investors(limit: 0, filters: $filters, subset: UNFILTERED) {
id
name
current_draft {
id
created_at
modified_at
modified_by {
id
username
}
}
created_at
created_by {
id
Expand Down Expand Up @@ -533,7 +571,6 @@
const name = combined_status_fn(o.status, o.draft_status, true);
const id = combined_status_fn(o.status, o.draft_status, false);
if (seen_ids.includes(id)) return;
console.log(o.status, o.draft_status);
status_options.push({ id, name });
seen_ids.push(id);
});
Expand All @@ -543,11 +580,6 @@
let objects = this.showDeals ? this.deals : this.investors;
if (!objects || objects.length === 0) return [];
objects = sortAnything(
objects.map((o) => ({ ...o, combined_status: [o.status, o.draft_status] })),
this.sortField,
this.sortAscending
);
if (this.user_region)
if (this.showDeals)
objects = objects.filter(
Expand All @@ -556,7 +588,6 @@
else
objects = objects.filter((o: Investor) => {
const deal_regions = o.deals.map((d) => d.country?.fk_region?.id);
console.log(deal_regions, this.user_region.id);
return deal_regions.includes(this.user_region.id);
});
if (this.user_country)
Expand All @@ -577,6 +608,7 @@
objects = objects.filter((o) => o.deal_size >= this.selected_from_size);
if (this.showDeals && this.selected_to_size)
objects = objects.filter((o) => o.deal_size <= this.selected_to_size);
if (this.created_daterange)
objects = objects.filter(
(o) =>
Expand All @@ -587,13 +619,17 @@
objects = objects.filter((o) => o.created_by?.id === this.created_by.id);
if (this.modified_daterange)
objects = objects.filter(
(o) =>
dayjs(this.modified_daterange.start).isSameOrBefore(
o.modified_at,
"day"
) &&
dayjs(this.modified_daterange.end).isSameOrAfter(o.modified_at, "day")
objects = objects.filter((o) =>
o.current_draft?.modified_at
? dayjs(this.modified_daterange.start).isSameOrBefore(
o.current_draft.modified_at,
"day"
) &&
dayjs(this.modified_daterange.end).isSameOrAfter(
o.current_draft.modified_at,
"day"
)
: false
);
if (this.modified_by)
objects = objects.filter((o) => o.modified_by?.id === this.modified_by.id);
Expand Down Expand Up @@ -636,8 +672,12 @@
);
break;
}
console.log(this.selected_combined_status);
}
objects = sortAnything(
objects.map((o) => ({ ...o, combined_status: [o.status, o.draft_status] })),
this.sortField,
this.sortAscending
);
return objects;
},
Expand Down Expand Up @@ -766,7 +806,7 @@
"country",
"deal_size",
"created_at",
"modified_at",
"current_draft.modified_at",
"fully_updated_at",
"workflowinfos",
"combined_status",
Expand All @@ -776,7 +816,7 @@
"name",
"country",
"created_at",
"modified_at",
"current_draft.modified_at",
"workflowinfos",
"combined_status",
"deals",
Expand Down
2 changes: 2 additions & 0 deletions newfront/src/lib/types/generics.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface ObjVersion {
id: number;
created_at: Date;
created_by: User;
modified_at: Date;
modified_by: User;
object_id: Int;
}

Expand Down

0 comments on commit 72dbaca

Please sign in to comment.