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 backenddb/pg_lookup_webhook_by_event.c
18 : * @brief Implementation of the lookup_webhook_by_event function for Postgres
19 : * @author Iván Ávalos
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_error_codes.h>
23 : #include <taler/taler_dbevents.h>
24 : #include <taler/taler_pq_lib.h>
25 : #include "pg_lookup_webhook_by_event.h"
26 : #include "pg_helper.h"
27 :
28 : /**
29 : * Context used for lookup_webhook_by_event_cb().
30 : */
31 : struct LookupWebhookDetailContext
32 : {
33 : /**
34 : * Function to call with the results.
35 : */
36 : TALER_MERCHANTDB_WebhookDetailCallback cb;
37 :
38 : /**
39 : * Closure for @a cb.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Did database result extraction fail?
45 : */
46 : bool extract_failed;
47 : };
48 :
49 : /**
50 : * Function to be called with the results of a SELECT statement
51 : * that has returned @a num_results results about webhook.
52 : *
53 : * @param[in,out] cls of type `struct LookupPendingWebhookContext *`
54 : * @param result the postgres result
55 : * @param num_results the number of results in @a result
56 : */
57 : static void
58 93 : lookup_webhook_by_event_cb (void *cls,
59 : PGresult *result,
60 : unsigned int num_results)
61 : {
62 93 : struct LookupWebhookDetailContext *wlc = cls;
63 :
64 95 : for (unsigned int i = 0; i < num_results; i++)
65 : {
66 : uint64_t webhook_serial;
67 : char *event_type;
68 : char *url;
69 : char *http_method;
70 : char *header_template;
71 : char *body_template;
72 2 : struct GNUNET_PQ_ResultSpec rs[] = {
73 2 : GNUNET_PQ_result_spec_uint64 ("webhook_serial",
74 : &webhook_serial),
75 2 : GNUNET_PQ_result_spec_string ("event_type",
76 : &event_type),
77 2 : GNUNET_PQ_result_spec_string ("url",
78 : &url),
79 2 : GNUNET_PQ_result_spec_string ("http_method",
80 : &http_method),
81 2 : GNUNET_PQ_result_spec_allow_null (
82 : GNUNET_PQ_result_spec_string ("header_template",
83 : &header_template),
84 : NULL),
85 2 : GNUNET_PQ_result_spec_allow_null (
86 : GNUNET_PQ_result_spec_string ("body_template",
87 : &body_template),
88 : NULL),
89 : GNUNET_PQ_result_spec_end
90 : };
91 :
92 2 : if (GNUNET_OK !=
93 2 : GNUNET_PQ_extract_result (result,
94 : rs,
95 : i))
96 : {
97 0 : GNUNET_break (0);
98 0 : wlc->extract_failed = true;
99 0 : return;
100 : }
101 2 : wlc->cb (wlc->cb_cls,
102 : webhook_serial,
103 : event_type,
104 : url,
105 : http_method,
106 : header_template,
107 : body_template);
108 2 : GNUNET_PQ_cleanup_result (rs);
109 : }
110 : }
111 :
112 : enum GNUNET_DB_QueryStatus
113 93 : TMH_PG_lookup_webhook_by_event (void *cls,
114 : const char *instance_id,
115 : const char *event_type,
116 : TALER_MERCHANTDB_WebhookDetailCallback cb,
117 : void *cb_cls)
118 : {
119 93 : struct PostgresClosure *pg = cls;
120 93 : struct LookupWebhookDetailContext wlc = {
121 : .cb = cb,
122 : .cb_cls = cb_cls,
123 : .extract_failed = false,
124 : };
125 :
126 93 : struct GNUNET_PQ_QueryParam params[] = {
127 93 : GNUNET_PQ_query_param_string (instance_id),
128 93 : GNUNET_PQ_query_param_string (event_type),
129 : GNUNET_PQ_query_param_end
130 : };
131 : enum GNUNET_DB_QueryStatus qs;
132 :
133 93 : check_connection (pg);
134 93 : PREPARE (pg,
135 : "lookup_webhook_by_event",
136 : "SELECT"
137 : " webhook_serial"
138 : ",event_type"
139 : ",url"
140 : ",http_method"
141 : ",header_template"
142 : ",body_template"
143 : " FROM merchant_webhook"
144 : " JOIN merchant_instances"
145 : " USING (merchant_serial)"
146 : " WHERE merchant_instances.merchant_id=$1"
147 : " AND event_type=$2");
148 :
149 93 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
150 : "lookup_webhook_by_event",
151 : params,
152 : &lookup_webhook_by_event_cb,
153 : &wlc);
154 :
155 93 : if (wlc.extract_failed)
156 0 : return GNUNET_DB_STATUS_HARD_ERROR;
157 93 : return qs;
158 : }
|