feat: implement save mechanisim
Signed-off-by: Sebastian <krupinski01@gmail.com>
This commit is contained in:
@@ -182,11 +182,92 @@ class CompositionManager {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function save(string $tenantId, string $userId, string $identifier, array $data): array {
|
||||
// Construct the snapshot mutation that will run while the composition is locked.
|
||||
$saveComposition = function (?array $composed) use ($data): ?array {
|
||||
if ($composed === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($data['revision']) && is_int($data['revision'])) {
|
||||
$composed['revision'] = $data['revision'];
|
||||
}
|
||||
$composed['sender'] = $data['sender'];
|
||||
$composed['message'] = $data['message'];
|
||||
return $this->markRemoteDirty($composed);
|
||||
};
|
||||
|
||||
// Execute the mutation and atomically persist the snapshot it returns.
|
||||
$composed = $this->compositionStore->compositionSave(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$identifier,
|
||||
$saveComposition,
|
||||
);
|
||||
if ($composed === null) {
|
||||
return [
|
||||
'identifier' => $identifier,
|
||||
'disposition' => 'error',
|
||||
'error' => [
|
||||
'type' => 'composition_not_found',
|
||||
'message' => 'Composition not found',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$synchronized = $this->synchronize($tenantId, $userId, $identifier);
|
||||
if ($synchronized['disposition'] !== 'saved') {
|
||||
return [
|
||||
'identifier' => $identifier,
|
||||
'disposition' => 'error',
|
||||
'error' => [
|
||||
'type' => 'composition_save_failed',
|
||||
'message' => (string)($synchronized['error'] ?? 'Draft could not be saved to the server'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$this->compositionStore->compositionDiscard($tenantId, $userId, $identifier);
|
||||
return ['identifier' => $identifier, 'disposition' => 'saved'];
|
||||
}
|
||||
|
||||
public function discard(string $tenantId, string $userId, string $identifier): array {
|
||||
return [
|
||||
'identifier' => $identifier,
|
||||
'disposition' => $this->compositionStore->compositionDiscard($tenantId, $userId, $identifier),
|
||||
];
|
||||
$composed = $this->compositionStore->compositionFetch($tenantId, $userId, $identifier);
|
||||
if ($composed === null) {
|
||||
return ['identifier' => $identifier, 'disposition' => 'discarded'];
|
||||
}
|
||||
|
||||
try {
|
||||
$remoteEntity = $this->remoteEntityIdentifier($composed['remote']['entity'] ?? null);
|
||||
if ($remoteEntity !== null) {
|
||||
$outcomes = $this->mailManager->entityDelete($tenantId, $userId, $remoteEntity);
|
||||
$outcome = $outcomes[(string)$remoteEntity] ?? reset($outcomes);
|
||||
if (!is_array($outcome) || ($outcome['disposition'] ?? 'error') === 'error') {
|
||||
return [
|
||||
'identifier' => $identifier,
|
||||
'disposition' => 'error',
|
||||
'error' => [
|
||||
'type' => 'composition_discard_failed',
|
||||
'message' => is_array($outcome)
|
||||
? (string)($outcome['error'] ?? 'Remote draft could not be discarded')
|
||||
: 'Remote draft could not be discarded',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
} catch (Throwable $throwable) {
|
||||
return [
|
||||
'identifier' => $identifier,
|
||||
'disposition' => 'error',
|
||||
'error' => [
|
||||
'type' => 'composition_discard_failed',
|
||||
'message' => $throwable->getMessage(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$this->compositionStore->compositionDiscard($tenantId, $userId, $identifier);
|
||||
return ['identifier' => $identifier, 'disposition' => 'discarded'];
|
||||
}
|
||||
|
||||
public function synchronize(string $tenantId, string $userId, string $identifier): array {
|
||||
|
||||
@@ -42,6 +42,7 @@ final class CompositionController extends ControllerAbstract {
|
||||
$result = match ($operation) {
|
||||
'stage' => $this->stage($tenantId, $userId, $data),
|
||||
'patch' => $this->patch($tenantId, $userId, $data),
|
||||
'save' => $this->save($tenantId, $userId, $data),
|
||||
'discard' => $this->discard($tenantId, $userId, $data),
|
||||
'send' => $this->send($tenantId, $userId, $data),
|
||||
'attachment.add' => $this->attachmentAdd($tenantId, $userId, $data),
|
||||
@@ -108,6 +109,21 @@ final class CompositionController extends ControllerAbstract {
|
||||
|
||||
return $this->compositionManager->patch($tenantId, $userId, $data['identifier'], $data);
|
||||
}
|
||||
|
||||
private function save(string $tenantId, string $userId, array $data): array {
|
||||
if (!isset($data['identifier']) || !is_string($data['identifier']) || $data['identifier'] === '') {
|
||||
throw new InvalidArgumentException('Invalid parameter: identifier must be a non-empty string');
|
||||
}
|
||||
if (!isset($data['sender']) || !is_array($data['sender'])) {
|
||||
throw new InvalidArgumentException('Invalid parameter: sender must be an array');
|
||||
}
|
||||
if (!isset($data['message']) || !is_array($data['message'])) {
|
||||
throw new InvalidArgumentException('Invalid parameter: message must be an array');
|
||||
}
|
||||
|
||||
return $this->compositionManager->save($tenantId, $userId, $data['identifier'], $data);
|
||||
}
|
||||
|
||||
private function discard(string $tenantId, string $userId, array $data): array {
|
||||
if (!isset($data['identifier']) || !is_string($data['identifier']) || $data['identifier'] === '') {
|
||||
throw new InvalidArgumentException('Invalid parameter: identifier must be a non-empty string');
|
||||
|
||||
Reference in New Issue
Block a user