|
| 1 | +import type { MethodHandler } from '../../types.js'; |
| 2 | +import type { RecordsDeleteMessage } from '../types.js'; |
| 3 | + |
| 4 | +import { authenticate } from '../../../core/auth.js'; |
| 5 | +import { deleteAllOlderMessagesButKeepInitialWrite } from '../records-interface.js'; |
| 6 | +import { DwnInterfaceName } from '../../../core/message.js'; |
| 7 | +import { MessageReply } from '../../../core/message-reply.js'; |
| 8 | +import { RecordsDelete } from '../messages/records-delete.js'; |
| 9 | +import { RecordsWrite } from '../messages/records-write.js'; |
| 10 | +import { TimestampedMessage } from '../../../core/types.js'; |
| 11 | + |
| 12 | +export const handleRecordsDelete: MethodHandler = async ( |
| 13 | + tenant, |
| 14 | + message, |
| 15 | + messageStore, |
| 16 | + didResolver |
| 17 | +): Promise<MessageReply> => { |
| 18 | + const incomingMessage = message as RecordsDeleteMessage; |
| 19 | + |
| 20 | + let recordsDelete: RecordsDelete; |
| 21 | + try { |
| 22 | + recordsDelete = await RecordsDelete.parse(incomingMessage); |
| 23 | + } catch (e) { |
| 24 | + return new MessageReply({ |
| 25 | + status: { code: 400, detail: e.message } |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + // authentication & authorization |
| 30 | + try { |
| 31 | + await authenticate(message.authorization, didResolver); |
| 32 | + await recordsDelete.authorize(tenant); |
| 33 | + } catch (e) { |
| 34 | + return new MessageReply({ |
| 35 | + status: { code: 401, detail: e.message } |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + // get existing records matching the `recordId` |
| 40 | + const query = { |
| 41 | + tenant, |
| 42 | + interface : DwnInterfaceName.Records, |
| 43 | + recordId : incomingMessage.descriptor.recordId |
| 44 | + }; |
| 45 | + const existingMessages = await messageStore.query(query) as TimestampedMessage[]; |
| 46 | + |
| 47 | + // find which message is the newest, and if the incoming message is the newest |
| 48 | + const newestExistingMessage = await RecordsWrite.getNewestMessage(existingMessages); |
| 49 | + let incomingMessageIsNewest = false; |
| 50 | + let newestMessage; |
| 51 | + // if incoming message is newest |
| 52 | + if (newestExistingMessage === undefined || await RecordsWrite.isNewer(incomingMessage, newestExistingMessage)) { |
| 53 | + incomingMessageIsNewest = true; |
| 54 | + newestMessage = incomingMessage; |
| 55 | + } else { // existing message is the same age or newer than the incoming message |
| 56 | + newestMessage = newestExistingMessage; |
| 57 | + } |
| 58 | + |
| 59 | + // write the incoming message to DB if incoming message is newest |
| 60 | + let messageReply: MessageReply; |
| 61 | + if (incomingMessageIsNewest) { |
| 62 | + const indexes = await constructIndexes(tenant, recordsDelete); |
| 63 | + |
| 64 | + await messageStore.put(incomingMessage, indexes); |
| 65 | + |
| 66 | + messageReply = new MessageReply({ |
| 67 | + status: { code: 202, detail: 'Accepted' } |
| 68 | + }); |
| 69 | + } else { |
| 70 | + messageReply = new MessageReply({ |
| 71 | + status: { code: 409, detail: 'Conflict' } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + // delete all existing messages that are not newest, except for the initial write |
| 76 | + await deleteAllOlderMessagesButKeepInitialWrite(tenant, existingMessages, newestMessage, messageStore); |
| 77 | + |
| 78 | + return messageReply; |
| 79 | +}; |
| 80 | + |
| 81 | +export async function constructIndexes(tenant: string, recordsDelete: RecordsDelete): Promise<{ [key: string]: string }> { |
| 82 | + const message = recordsDelete.message; |
| 83 | + const descriptor = { ...message.descriptor }; |
| 84 | + |
| 85 | + // NOTE: the "trick" not may not be apparent on how a query is able to omit deleted records: |
| 86 | + // we intentionally not add index for `isLatestBaseState` at all, this means that upon a successful delete, |
| 87 | + // no messages with the record ID will match any query because queries by design filter by `isLatestBaseState = true`, |
| 88 | + // `isLatestBaseState` for the initial delete would have been toggled to `false` |
| 89 | + const indexes: { [key: string]: any } = { |
| 90 | + tenant, |
| 91 | + // isLatestBaseState : "true", // intentionally showing that this index is omitted |
| 92 | + author: recordsDelete.author, |
| 93 | + ...descriptor |
| 94 | + }; |
| 95 | + |
| 96 | + return indexes; |
| 97 | +} |
0 commit comments