Skip to content

Commit

Permalink
feat: valid group start (#1441)
Browse files Browse the repository at this point in the history
Signed-off-by: Hristiyan <[email protected]>
Signed-off-by: Svetoslav Borislavov <[email protected]>
Co-authored-by: Svetoslav Borislavov <[email protected]>
Co-authored-by: John Bair <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent 8abd3c3 commit d4feb24
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class TransactionGroupDto {
@Expose()
createdAt: Date;

@Expose()
groupValidTime: Date;

@Expose()
@Type(() => TransactionGroupItemDto)
groupItems: TransactionGroupItemDto[];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_TransactionGroup" (
"id" TEXT NOT NULL PRIMARY KEY,
"description" TEXT NOT NULL,
"atomic" BOOLEAN NOT NULL,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"groupValidStart" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO "new_TransactionGroup" ("atomic", "created_at", "description", "id") SELECT "atomic", "created_at", "description", "id" FROM "TransactionGroup";
DROP TABLE "TransactionGroup";
ALTER TABLE "new_TransactionGroup" RENAME TO "TransactionGroup";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
1 change: 1 addition & 0 deletions front-end/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ model TransactionGroup {
description String
atomic Boolean
created_at DateTime @default(now())
groupValidStart DateTime @default(now())
GroupItem GroupItem[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
isLoggedInOrganization,
isUserLoggedIn,
getErrorMessage,
assertIsLoggedInOrganization,
} from '@renderer/utils';
import AppButton from '@renderer/components/ui/AppButton.vue';
Expand Down Expand Up @@ -120,12 +121,7 @@ async function signAfterConfirm() {
}
assertUserLoggedIn(user.personal);
if (user.selectedOrganization) {
throw new Error(
"User is in organization mode, shouldn't be able to sign before submitting to organization",
);
}
assertIsLoggedInOrganization(user.selectedOrganization);
/* Verifies the user has entered his password */
const personalPassword = getPassword(signAfterConfirm, {
Expand Down Expand Up @@ -269,7 +265,7 @@ async function executeTransaction(transactionBytes: Uint8Array, groupItem?: Grou
await deleteDraft(savedGroupItem.transaction_draft_id!);
} else if (groupItem) {
if (newGroupId.value === '') {
const newGroup = await addGroup('', false);
const newGroup = await addGroup('', false, transactionGroup.groupValidStart);
newGroupId.value = newGroup.id;
}
await addGroupItem(groupItem, newGroupId.value, storedTransaction.id);
Expand All @@ -288,7 +284,7 @@ async function sendSignedTransactionsToOrganization() {
/* Verifies the user has entered his password */
assertUserLoggedIn(user.personal);
const personalPassword = getPassword(signAfterConfirm, {
const personalPassword = getPassword(sendSignedTransactionsToOrganization, {
subHeading: 'Enter your application password to sign as a creator',
});
if (passwordModalOpened(personalPassword)) return;
Expand Down Expand Up @@ -336,6 +332,7 @@ async function sendSignedTransactionsToOrganization() {
transactionGroup.description,
false,
transactionGroup.sequential,
transactionGroup.groupValidStart,
apiGroupItems,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import EmptyTransactions from '@renderer/components/EmptyTransactions.vue';
import TransactionSelectionModal from '@renderer/components/TransactionSelectionModal.vue';
import TransactionGroupProcessor from '@renderer/components/Transaction/TransactionGroupProcessor.vue';
import SaveTransactionGroupModal from '@renderer/components/modals/SaveTransactionGroupModal.vue';
import RunningClockDatePicker from '@renderer/components/RunningClockDatePicker.vue';
/* Stores */
const transactionGroup = useTransactionGroupStore();
Expand Down Expand Up @@ -68,7 +69,11 @@ async function saveTransactionGroup() {
throw new Error('Please add at least one transaction to the group');
}
await transactionGroup.saveGroup(user.personal.id, groupDescription.value);
await transactionGroup.saveGroup(
user.personal.id,
groupDescription.value,
transactionGroup.groupValidStart,
);
transactionGroup.clearGroup();
}
async function handleSaveGroup() {
Expand Down Expand Up @@ -155,6 +160,7 @@ async function handleSignSubmit() {
}
try {
transactionGroup.updateTransactionValidStarts(transactionGroup.groupValidStart);
const ownerKeys = new Array<PublicKey>();
for (const key of user.keyPairs) {
ownerKeys.push(PublicKey.fromString(key.public_key));
Expand Down Expand Up @@ -292,6 +298,10 @@ async function handleOnFileChanged(e: Event) {
}
}
function updateGroupValidStart(newDate: Date) {
transactionGroup.groupValidStart = newDate;
}
/* Functions */
function makeTransfer(index: number) {
const transfers = (
Expand Down Expand Up @@ -429,12 +439,24 @@ onBeforeRouteLeave(async to => {
</div>
<hr class="separator my-5 w-100" />
<div v-if="!groupEmpty" class="fill-remaining pb-10">
<div class="text-end mb-5">
{{
transactionGroup.groupItems.length < 2
? `1 Transaction`
: `${transactionGroup.groupItems.length} Transactions`
}}
<div class="d-flex justify-content-between align-items-center mb-5">
<div>
<label class="form-label"
>Group Valid Start <span class="text-muted text-italic">- Local time</span></label
>
<RunningClockDatePicker
:model-value="transactionGroup.groupValidStart"
@update:modelValue="updateGroupValidStart"
:nowButtonVisible="true"
/>
</div>
<div>
{{
transactionGroup.groupItems.length < 2
? `1 Transaction`
: `${transactionGroup.groupItems.length} Transactions`
}}
</div>
</div>
<div
v-for="(groupItem, index) in transactionGroup.groupItems"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IGroup {
atomic: boolean;
sequential: boolean;
createdAt: string;
groupValidStart: Date;
groupItems: IGroupItem[];
}

Expand All @@ -26,6 +27,7 @@ export const submitTransactionGroup = async (
description: string,
atomic: boolean,
sequential: boolean,
groupValidStart: Date,
groupItems: ApiGroupItem[],
): Promise<{ id: number; transactionBytes: string }> => {
return commonRequestHandler(async () => {
Expand All @@ -35,6 +37,7 @@ export const submitTransactionGroup = async (
description,
atomic,
sequential,
groupValidStart,
groupItems,
},
{
Expand Down
6 changes: 4 additions & 2 deletions front-end/src/renderer/services/transactionGroupsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export const addGroupWithDrafts = async (
description: string,
atomic: boolean,
groupItems: StoreGroupItem[],
groupValidStart: Date,
details?: string,
) => {
const group = await addGroup(description, atomic);
const group = await addGroup(description, atomic, groupValidStart);
try {
for (const [index, item] of groupItems.entries()) {
const transactionDraft: Prisma.TransactionDraftUncheckedCreateInput = {
Expand Down Expand Up @@ -96,12 +97,13 @@ export async function addGroupItem(
}
}

export async function addGroup(description: string, atomic: boolean) {
export async function addGroup(description: string, atomic: boolean, groupValidStart: Date) {
const transactionGroup: Prisma.TransactionGroupUncheckedCreateInput = {
description,
// Not sure how this should be entered or what it's for
atomic: atomic,
created_at: new Date(),
groupValidStart: groupValidStart,
};
try {
return await window.electronAPI.local.transactionGroups.addGroup(transactionGroup);
Expand Down
47 changes: 44 additions & 3 deletions front-end/src/renderer/stores/storeTransactionGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface GroupItem {
const useTransactionGroupStore = defineStore('transactionGroup', () => {
/* State */
const groupItems = ref<GroupItem[]>([]);
const groupValidStart = ref(new Date());
const description = ref('');
const sequential = ref(false);
const modified = ref(false);
Expand All @@ -42,9 +43,18 @@ const useTransactionGroupStore = defineStore('transactionGroup', () => {
if (modified.value) {
return;
}

groupItems.value = [];
const group = await getGroup(id);
description.value = group.description;
if (group.groupValidStart) {
if (group.groupValidStart > groupValidStart.value) {
groupValidStart.value = group.groupValidStart;
} else {
groupValidStart.value = new Date();
}
}

const items = await getGroupItems(id);
const drafts = await getDrafts(findArgs);
const groupItemsToAdd: GroupItem[] = [];
Expand Down Expand Up @@ -73,6 +83,7 @@ const useTransactionGroupStore = defineStore('transactionGroup', () => {

function clearGroup() {
groupItems.value = [];
groupValidStart.value = new Date();
description.value = '';
sequential.value = false;
modified.value = false;
Expand Down Expand Up @@ -161,10 +172,16 @@ const useTransactionGroupStore = defineStore('transactionGroup', () => {
return new Date(validStartMillis);
}

async function saveGroup(userId: string, description: string) {
async function saveGroup(userId: string, description: string, groupValidStart: Date) {
// Alter this when we know what 'atomic' does
if (groupItems.value[0].groupId === undefined) {
const newGroupId = await addGroupWithDrafts(userId, description, false, groupItems.value);
const newGroupId = await addGroupWithDrafts(
userId,
description,
false,
groupItems.value,
groupValidStart,
);
const items = await getGroupItems(newGroupId!);
for (const [index, groupItem] of groupItems.value.entries()) {
groupItem.groupId = newGroupId;
Expand All @@ -174,7 +191,7 @@ const useTransactionGroupStore = defineStore('transactionGroup', () => {
await updateGroup(
groupItems.value[0].groupId,
userId,
{ description, atomic: false },
{ description, atomic: false, groupValidStart: groupValidStart },
groupItems.value,
);
}
Expand Down Expand Up @@ -221,6 +238,28 @@ const useTransactionGroupStore = defineStore('transactionGroup', () => {
return modified.value;
}

function updateTransactionValidStarts(newGroupValidStart: Date) {
groupItems.value = groupItems.value.map((item, index) => {
const now = new Date();
if (item.validStart < now) {
const updatedValidStart = findUniqueValidStart(
item.payerAccountId,
newGroupValidStart.getTime() + index,
);
const transaction = Transaction.fromBytes(item.transactionBytes);
transaction.setTransactionId(createTransactionId(item.payerAccountId, updatedValidStart));

return {
...item,
transactionBytes: transaction.toBytes(),
validStart: updatedValidStart,
};
}
return item;
});
setModified();
}

// function getObservers() {
// const observers = new Array<number>();
// for (const groupItem of groupItems.value) {
Expand All @@ -244,13 +283,15 @@ const useTransactionGroupStore = defineStore('transactionGroup', () => {
clearGroup,
groupItems,
description,
groupValidStart,
sequential,
getRequiredKeys,
editGroupItem,
hasObservers,
hasApprovers,
setModified,
isModified,
updateTransactionValidStarts,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('Transaction Groups Service', () => {
description: 'description',
atomic: false,
created_at: new Date(),
groupValidStart: new Date(),
};

prisma.transactionGroup.findUnique.mockResolvedValue(group);
Expand Down Expand Up @@ -166,6 +167,7 @@ describe('Transaction Groups Service', () => {
const updateData = {
description: 'new description',
atomic: true,
groupValidDate: new Date(),
};

await updateGroup(id, updateData);
Expand Down

0 comments on commit d4feb24

Please sign in to comment.