|
14 | 14 | use Appwrite\Services\Users; |
15 | 15 | use Override; |
16 | 16 | use Utopia\Database\Database as UtopiaDatabase; |
| 17 | +use Utopia\Database\DateTime; |
17 | 18 | use Utopia\Database\Document as UtopiaDocument; |
18 | 19 | use Utopia\Database\Exception as DatabaseException; |
19 | 20 | use Utopia\Database\Exception\Authorization as AuthorizationException; |
@@ -942,10 +943,37 @@ protected function createRow(Row $resource, bool $isLast): bool |
942 | 943 | return false; |
943 | 944 | } |
944 | 945 |
|
| 946 | + $data = $resource->getData(); |
| 947 | + |
| 948 | + $hasCreatedAt = !empty($data['$createdAt']); |
| 949 | + $hasUpdatedAt = !empty($data['$updatedAt']); |
| 950 | + |
| 951 | + if (! $hasCreatedAt) { |
| 952 | + $createdAt = $resource->getCreatedAt(); |
| 953 | + if (empty($createdAt)) { |
| 954 | + $createdAt = $data['created_at'] ?? $data['createdAt'] ?? null; |
| 955 | + } |
| 956 | + |
| 957 | + $data['$createdAt'] = $this->normalizeDateTime($createdAt); |
| 958 | + } |
| 959 | + |
| 960 | + if (! $hasUpdatedAt) { |
| 961 | + $updatedAt = $resource->getUpdatedAt(); |
| 962 | + if (empty($updatedAt)) { |
| 963 | + $updatedAt = $data['updated_at'] ?? $data['updatedAt'] ?? null; |
| 964 | + } |
| 965 | + |
| 966 | + if (empty($updatedAt)) { |
| 967 | + $data['$updatedAt'] = (string) $data['$createdAt']; |
| 968 | + } else { |
| 969 | + $data['$updatedAt'] = $this->normalizeDateTime($updatedAt, $data['$createdAt']); |
| 970 | + } |
| 971 | + } |
| 972 | + |
945 | 973 | $this->rowBuffer[] = new UtopiaDocument(\array_merge([ |
946 | 974 | '$id' => $resource->getId(), |
947 | 975 | '$permissions' => $resource->getPermissions(), |
948 | | - ], $resource->getData())); |
| 976 | + ], $data)); |
949 | 977 |
|
950 | 978 | if ($isLast) { |
951 | 979 | try { |
@@ -1001,6 +1029,37 @@ protected function createRow(Row $resource, bool $isLast): bool |
1001 | 1029 | return true; |
1002 | 1030 | } |
1003 | 1031 |
|
| 1032 | + /** |
| 1033 | + * @throws \Exception |
| 1034 | + */ |
| 1035 | + private function normalizeDateTime(mixed $value, mixed $fallback = null): string |
| 1036 | + { |
| 1037 | + $resolved = $this->stringifyDateValue($value) |
| 1038 | + ?? $this->stringifyDateValue($fallback); |
| 1039 | + |
| 1040 | + if (empty($resolved)) { |
| 1041 | + return DateTime::format(new \DateTime()); |
| 1042 | + } |
| 1043 | + |
| 1044 | + if (\is_numeric($resolved) && \strlen($resolved) === 10 && (int) $resolved > 0) { // Unix timestamp |
| 1045 | + $resolved = '@' . $resolved; |
| 1046 | + } |
| 1047 | + |
| 1048 | + return DateTime::format(new \DateTime($resolved)); |
| 1049 | + } |
| 1050 | + |
| 1051 | + private function stringifyDateValue(mixed $value): ?string |
| 1052 | + { |
| 1053 | + if (\is_string($value)) { |
| 1054 | + return $value; |
| 1055 | + } |
| 1056 | + if (\is_int($value) || \is_float($value)) { |
| 1057 | + return (string) $value; |
| 1058 | + } |
| 1059 | + |
| 1060 | + return null; |
| 1061 | + } |
| 1062 | + |
1004 | 1063 | /** |
1005 | 1064 | * @throws AppwriteException |
1006 | 1065 | */ |
|
0 commit comments