Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file src/backenddb/iterate_webhooks_by_event.c
18 : * @brief Implementation of the iterate_webhooks_by_event function for Postgres
19 : * @author Iván Ávalos
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_webhooks_by_event.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Context used for lookup_webhook_by_event_cb().
28 : */
29 : struct LookupWebhookDetailContext
30 : {
31 : /**
32 : * Function to call with the results.
33 : */
34 : TALER_MERCHANTDB_WebhookDetailCallback cb;
35 :
36 : /**
37 : * Closure for @a cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Did database result extraction fail?
43 : */
44 : bool extract_failed;
45 : };
46 :
47 : /**
48 : * Function to be called with the results of a SELECT statement
49 : * that has returned @a num_results results about webhook.
50 : *
51 : * @param[in,out] cls of type `struct LookupPendingWebhookContext *`
52 : * @param result the postgres result
53 : * @param num_results the number of results in @a result
54 : */
55 : static void
56 156 : lookup_webhook_by_event_cb (void *cls,
57 : PGresult *result,
58 : unsigned int num_results)
59 : {
60 156 : struct LookupWebhookDetailContext *wlc = cls;
61 :
62 164 : for (unsigned int i = 0; i < num_results; i++)
63 : {
64 : uint64_t webhook_serial;
65 : char *event_type;
66 : char *url;
67 : char *http_method;
68 : char *header_template;
69 : char *body_template;
70 8 : struct GNUNET_PQ_ResultSpec rs[] = {
71 8 : GNUNET_PQ_result_spec_uint64 ("webhook_serial",
72 : &webhook_serial),
73 8 : GNUNET_PQ_result_spec_string ("event_type",
74 : &event_type),
75 8 : GNUNET_PQ_result_spec_string ("url",
76 : &url),
77 8 : GNUNET_PQ_result_spec_string ("http_method",
78 : &http_method),
79 8 : GNUNET_PQ_result_spec_allow_null (
80 : GNUNET_PQ_result_spec_string ("header_template",
81 : &header_template),
82 : NULL),
83 8 : GNUNET_PQ_result_spec_allow_null (
84 : GNUNET_PQ_result_spec_string ("body_template",
85 : &body_template),
86 : NULL),
87 : GNUNET_PQ_result_spec_end
88 : };
89 :
90 8 : if (GNUNET_OK !=
91 8 : GNUNET_PQ_extract_result (result,
92 : rs,
93 : i))
94 : {
95 0 : GNUNET_break (0);
96 0 : wlc->extract_failed = true;
97 0 : return;
98 : }
99 8 : wlc->cb (wlc->cb_cls,
100 : webhook_serial,
101 : event_type,
102 : url,
103 : http_method,
104 : header_template,
105 : body_template);
106 8 : GNUNET_PQ_cleanup_result (rs);
107 : }
108 : }
109 :
110 :
111 : enum GNUNET_DB_QueryStatus
112 156 : TALER_MERCHANTDB_iterate_webhooks_by_event (
113 : struct TALER_MERCHANTDB_PostgresContext *pg,
114 : const char *instance_id,
115 : const char *event_type,
116 : TALER_MERCHANTDB_WebhookDetailCallback cb,
117 : void *cb_cls)
118 : {
119 156 : struct LookupWebhookDetailContext wlc = {
120 : .cb = cb,
121 : .cb_cls = cb_cls,
122 : .extract_failed = false,
123 : };
124 :
125 156 : struct GNUNET_PQ_QueryParam params[] = {
126 156 : GNUNET_PQ_query_param_string (event_type),
127 : GNUNET_PQ_query_param_end
128 : };
129 : enum GNUNET_DB_QueryStatus qs;
130 :
131 156 : GNUNET_assert (NULL != pg->current_merchant_id);
132 156 : GNUNET_assert (0 == strcmp (instance_id,
133 : pg->current_merchant_id));
134 156 : TMH_PQ_prepare_anon (pg,
135 : "SELECT"
136 : " webhook_serial"
137 : ",event_type"
138 : ",url"
139 : ",http_method"
140 : ",header_template"
141 : ",body_template"
142 : " FROM merchant_webhook"
143 : " WHERE event_type=$1");
144 :
145 156 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
146 : "",
147 : params,
148 : &lookup_webhook_by_event_cb,
149 : &wlc);
150 :
151 156 : if (wlc.extract_failed)
152 0 : return GNUNET_DB_STATUS_HARD_ERROR;
153 156 : return qs;
154 : }
|