(core) Adds endpoint to update webhook

Summary:
Adds a new endpoint to update webhook.

Perform some refactoring to allow code reuse from endpoint allowing to _subscribe and _unsubscribe webhooks.

One aspect of webhook is that url are stored in the home db while the rest of the fields (tableRef, isReadyColRef, ...) are stored in sqlite. So care must be taken when updating fields, to properly rollback if anything should fail.

Follow up diff will bring UI to edit webhook list

Test Plan: Updated doc api server tests

Reviewers: jarek

Reviewed By: jarek

Subscribers: paulfitz

Differential Revision: https://phab.getgrist.com/D3821
This commit is contained in:
Cyprien P
2023-03-01 21:43:22 +01:00
parent 8cb928e83d
commit d8a063284a
8 changed files with 432 additions and 61 deletions

View File

@@ -1851,6 +1851,18 @@ export class HomeDBManager extends EventEmitter {
});
}
// Updates the secret matching id and docId, to the new value.
public async updateSecret(id: string, docId: string, value: string, manager?: EntityManager): Promise<void> {
const res = await (manager || this._connection).createQueryBuilder()
.update(Secret)
.set({value})
.where("id = :id AND doc_id = :docId", {id, docId})
.execute();
if (res.affected !== 1) {
throw new ApiError('secret with given id not found', 404);
}
}
public async getSecret(id: string, docId: string, manager?: EntityManager): Promise<string | undefined> {
const secret = await (manager || this._connection).createQueryBuilder()
.select('secrets')
@@ -1860,6 +1872,20 @@ export class HomeDBManager extends EventEmitter {
return secret?.value;
}
// Update the webhook url in the webhook's corresponding secret (note: the webhook identifier is
// its secret identifier).
public async updateWebhookUrl(id: string, docId: string, url: string, outerManager?: EntityManager) {
return await this._runInTransaction(outerManager, async manager => {
const value = await this.getSecret(id, docId, manager);
if (!value) {
throw new ApiError('Webhook with given id not found', 404);
}
const webhookSecret = JSON.parse(value);
webhookSecret.url = url;
await this.updateSecret(id, docId, JSON.stringify(webhookSecret), manager);
});
}
public async removeWebhook(id: string, docId: string, unsubscribeKey: string, checkKey: boolean): Promise<void> {
if (!id) {
throw new ApiError('Bad request: id required', 400);
@@ -1881,7 +1907,7 @@ export class HomeDBManager extends EventEmitter {
await manager.createQueryBuilder()
.delete()
.from(Secret)
.where('id = :id', {id})
.where('id = :id AND doc_id = :docId', {id, docId})
.execute();
});
}