signature: check for signature state

This commit is contained in:
nobohan 2024-07-12 11:52:22 +02:00
parent 77da2c1ac6
commit 421226c0dc
3 changed files with 85 additions and 34 deletions

View File

@ -17,6 +17,8 @@ use Chill\DocStoreBundle\Service\Signature\PDFPage;
use Chill\DocStoreBundle\Service\Signature\PDFSignatureZone;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowSignatureStateEnum;
use Chill\MainBundle\Workflow\EntityWorkflowManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\MessageBusInterface;
@ -27,26 +29,20 @@ class SignatureRequestController
public function __construct(
private MessageBusInterface $messageBus,
private StoredObjectManagerInterface $storedObjectManager,
private EntityWorkflowManager $entityWorkflowManager,
) {}
#[Route('/api/1.0/document/workflow/{id}/signature-request', name: 'chill_docstore_signature_request')]
//public function processSignature(EntityWorkflowStepSignature $signature): Response // using ParamConverter
public function processSignature(EntityWorkflowStepSignature $signature, Request $request): JsonResponse
{
dump($signature);
// $signature = $this->signatureRepository($signature); //not useful?
// $entityWorkflow = $signature->getStep()->getEntityWorkflow();
// $storedObject = $this->entityWorkflowManager->getAssociatedStoredObject($entityWorkflow);
$content = 'blop';// $this->storedObjectManager->read($storedObject);
dump($request);
// TODO parse payload: json_decode ou, mieux, dataTransfertObject
// dump($request);
$data = \json_decode((string) $request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$data = \json_decode((string) $request->getContent(), true, 512, JSON_THROW_ON_ERROR); // TODO parse payload: json_decode ou, mieux, dataTransfertObject
dump($data);
$signatureId = 1;
$storedObjectId = 9; //$data['storedObject']['id'];
$content = 'blop';// $this->storedObjectManager->read($storedObjectId);
$zone = new PDFSignatureZone(
$data['zone']['index'],
$data['zone']['x'],
@ -57,7 +53,7 @@ class SignatureRequestController
);
$this->messageBus->dispatch(new RequestPdfSignMessage(
$signatureId,
$signature->getId(),
$zone,
$data['zone']['index'],
'test signature', //reason (string)
@ -67,4 +63,10 @@ class SignatureRequestController
return new JsonResponse(null, JsonResponse::HTTP_OK, []);
}
#[Route('/api/1.0/document/workflow/{id}/check-signature', name: 'chill_docstore_check_signature')]
public function checkSignature(EntityWorkflowStepSignature $signature): JsonResponse
{
return new JsonResponse($signature->getState(), JsonResponse::HTTP_OK, []);
}
}

View File

@ -80,4 +80,6 @@ export interface Signature {
id: number,
storedObject: StoredObject,
zones: SignatureZone[],
}
}
export type SignedState = 'pending' | 'signed' | 'rejected' | 'canceled' | 'error';

View File

@ -32,7 +32,11 @@
v-if="signature.zones.length > 1"
>
<div class="col-4 gap-2 d-grid">
<button :disabled="zone < 1" class="btn btn-light btn-sm" @click="turn_signature(-1)">
<button
:disabled="zone < 1"
class="btn btn-light btn-sm"
@click="turn_signature(-1)"
>
{{ $t("last_sign_zone") }}
</button>
</div>
@ -71,7 +75,7 @@
<canvas class="m-auto" id="canvas"></canvas>
</div>
<div class="col-12 p-4" id="action-buttons" v-if="!signed">
<div class="col-12 p-4" id="action-buttons" v-if="signedState !== 'signed'">
<div class="row">
<div class="col-6">
<button
@ -109,7 +113,7 @@
<script setup lang="ts">
import { ref, Ref, reactive } from "vue";
import { Signature, SignatureZone } from "../../types";
import { Signature, SignatureZone, SignedState } from "../../types";
import { makeFetch } from "../../../../../ChillMainBundle/Resources/public/lib/api/apiMethods";
import * as pdfjsLib from "pdfjs-dist";
import {
@ -134,7 +138,7 @@ pdfjsLib.GlobalWorkerOptions.workerSrc = "pdfjs-dist/build/pdf.worker.mjs";
const modalOpen: Ref<boolean> = ref(false);
const loading: Ref<boolean> = ref(false);
const signed: Ref<boolean> = ref(false);
const signedState: Ref<SignedState> = ref("pending");
const page: Ref<number> = ref(1);
const pageCount: Ref<number> = ref(0);
const zone: Ref<number> = ref(0);
@ -235,10 +239,7 @@ const canvas_click = (e: PointerEvent, canvas: HTMLCanvasElement) =>
const ctx = canvas.getContext("2d");
if (ctx) {
set_page(page.value);
setTimeout(
() => draw_zone(z, ctx, canvas.width, canvas.height),
200
);
setTimeout(() => draw_zone(z, ctx, canvas.width, canvas.height), 200);
}
userSignatureZones.value = z;
}
@ -318,28 +319,74 @@ const add_zones = (page: number) => {
}
};
const check_signature = () => {
const url = `/api/1.0/document/workflow/${signature.id}/check-signature`;
return makeFetch("GET", url)
.then((r) => {
signedState.value = r as SignedState;
if (signedState.value === "pending") {
check_for_ready();
}
})
.catch((error) => {
signedState.value = "error";
console.log('Error while checking the signature', error);
//TODO toast error
});
};
const maxTryForReady = 60; //2 minutes for trying to sign
let tryForReady = 0;
const stop_try_signing = () => {
loading.value = false;
modalOpen.value = false;
};
const check_for_ready = () => {
if (tryForReady > maxTryForReady) {
signedState.value = "error";
stop_try_signing();
console.log("Reached the maximum number of tentative to try signing");
//TODO toast error
}
if (signedState.value === "rejected") {
stop_try_signing();
console.log("Signature rejected by the server");
//TODO toast error
}
if (signedState.value === "canceled") {
stop_try_signing();
console.log("Signature canceledconsole.log('Error while posting the signature', error);");
//TODO toast error
}
if (signedState.value === "pending") {
tryForReady = tryForReady + 1;
setTimeout(() => check_signature(), 2000);
} else {
stop_try_signing();
if (signedState.value === "signed") {
//TODO recharger le document signé
}
}
};
const confirm_sign = () => {
console.log(userSignatureZones.value);
loading.value = true;
//TODO POST userSignatureZones to backend
const url = `/api/1.0/document/workflow/${signature.id}/signature-request`;
const body = {
storedObject: signature.storedObject,
zone: userSignatureZones.value,
};
makeFetch('POST', url, body)
.then(r => {
})
.catch((error) => {
});
// setTimeout(() => {
// loading.value = false;
// modalOpen.value = false;
// signed.value = true;
// }, 3000); // FAKE
makeFetch("POST", url, body)
.then((r) => {
check_for_ready();
})
.catch((error) => {
console.log('Error while posting the signature', error);
stop_try_signing();
//TODO toast
});
};
const undo_sign = async () => {