ameliabooking
/
src
/
Infrastructure
/
Repository
/
Notification
/
NotificationSMSHistoryRepository.php
NotificationLogRepository.php
1 year ago
NotificationRepository.php
2 years ago
NotificationSMSHistoryRepository.php
2 years ago
NotificationsToEntitiesRepository.php
4 years ago
NotificationSMSHistoryRepository.php
254 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Notification; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 6 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 7 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 8 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 9 | |
| 10 | /** |
| 11 | * Class NotificationSMSHistoryRepository |
| 12 | * |
| 13 | * @package AmeliaBooking\Infrastructure\Repository\Notification |
| 14 | */ |
| 15 | class NotificationSMSHistoryRepository extends AbstractRepository |
| 16 | { |
| 17 | /** |
| 18 | * @param $data |
| 19 | * |
| 20 | * @return bool |
| 21 | * |
| 22 | * @throws QueryExecutionException |
| 23 | * @throws \Exception |
| 24 | */ |
| 25 | public function add($data) |
| 26 | { |
| 27 | $params = [ |
| 28 | ':notificationId' => $data['notificationId'], |
| 29 | ':userId' => $data['userId'], |
| 30 | ':appointmentId' => !empty($data['appointmentId']) ? $data['appointmentId'] : null, |
| 31 | ':eventId' => !empty($data['eventId']) ? $data['eventId'] : null, |
| 32 | ':packageCustomerId' => !empty($data['packageCustomerId']) ? $data['packageCustomerId'] : null, |
| 33 | ':text' => $data['text'], |
| 34 | ':phone' => $data['phone'], |
| 35 | ':alphaSenderId' => $data['alphaSenderId'] |
| 36 | ]; |
| 37 | |
| 38 | try { |
| 39 | $statement = $this->connection->prepare( |
| 40 | "INSERT INTO {$this->table} |
| 41 | ( |
| 42 | `notificationId`, |
| 43 | `userId`, |
| 44 | `appointmentId`, |
| 45 | `eventId`, |
| 46 | `packageCustomerId`, |
| 47 | `text`, |
| 48 | `phone`, |
| 49 | `alphaSenderId` |
| 50 | ) |
| 51 | VALUES |
| 52 | ( |
| 53 | :notificationId, |
| 54 | :userId, |
| 55 | :appointmentId, |
| 56 | :eventId, |
| 57 | :packageCustomerId, |
| 58 | :text, |
| 59 | :phone, |
| 60 | :alphaSenderId |
| 61 | )" |
| 62 | ); |
| 63 | |
| 64 | $res = $statement->execute($params); |
| 65 | if (!$res) { |
| 66 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 67 | } |
| 68 | |
| 69 | return $this->connection->lastInsertId(); |
| 70 | } catch (\Exception $e) { |
| 71 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param $id |
| 77 | * @param $data |
| 78 | * |
| 79 | * @return bool |
| 80 | * @throws QueryExecutionException |
| 81 | */ |
| 82 | public function update($id, $data) |
| 83 | { |
| 84 | $params = [ |
| 85 | ':status' => $data['status'], |
| 86 | ':price' => $data['price'], |
| 87 | ':id' => $id |
| 88 | ]; |
| 89 | |
| 90 | $sqlUpdate = ''; |
| 91 | |
| 92 | if (isset($data['logId'])) { |
| 93 | $params[':logId'] = $data['logId']; |
| 94 | |
| 95 | $sqlUpdate .= '`logId` = COALESCE(:logId, `logId`),'; |
| 96 | } |
| 97 | |
| 98 | if (isset($data['dateTime'])) { |
| 99 | $params[':dateTime'] = $data['dateTime']; |
| 100 | |
| 101 | $sqlUpdate .= '`dateTime` = COALESCE(:dateTime, `dateTime`),'; |
| 102 | } |
| 103 | |
| 104 | if (isset($data['segments'])) { |
| 105 | $params[':segments'] = $data['segments']; |
| 106 | |
| 107 | $sqlUpdate .= '`segments` = COALESCE(:segments, `segments`),'; |
| 108 | } |
| 109 | |
| 110 | try { |
| 111 | $statement = $this->connection->prepare( |
| 112 | "UPDATE {$this->table} SET |
| 113 | {$sqlUpdate} |
| 114 | `status` = COALESCE(:status, `status`), |
| 115 | `price` = COALESCE(:price, `price`) |
| 116 | WHERE id = :id" |
| 117 | ); |
| 118 | |
| 119 | $res = $statement->execute($params); |
| 120 | |
| 121 | if (!$res) { |
| 122 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 123 | } |
| 124 | |
| 125 | return $res; |
| 126 | } catch (\Exception $e) { |
| 127 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param int $id |
| 133 | * |
| 134 | * @return array |
| 135 | * @throws QueryExecutionException |
| 136 | */ |
| 137 | public function getById($id) |
| 138 | { |
| 139 | try { |
| 140 | $statement = $this->connection->prepare( |
| 141 | $this->selectQuery() . " WHERE id = :id" |
| 142 | ); |
| 143 | |
| 144 | $params = [ |
| 145 | ':id' => $id, |
| 146 | ]; |
| 147 | |
| 148 | $statement->execute($params); |
| 149 | |
| 150 | $row = $statement->fetch(); |
| 151 | } catch (\Exception $e) { |
| 152 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 153 | } |
| 154 | |
| 155 | if (!$row) { |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | return $row; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param $criteria |
| 164 | * @param $itemsPerPage |
| 165 | * |
| 166 | * @return array |
| 167 | * @throws QueryExecutionException |
| 168 | */ |
| 169 | public function getFiltered($criteria, $itemsPerPage) |
| 170 | { |
| 171 | try { |
| 172 | $params = []; |
| 173 | $where = []; |
| 174 | |
| 175 | if (!empty($criteria['dates'])) { |
| 176 | $where[] = "(DATE_FORMAT(h.dateTime, '%Y-%m-%d %H:%i:%s') BETWEEN :dateFrom AND :dateTo)"; |
| 177 | $params[':dateFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 178 | $params[':dateTo'] = DateTimeService::getCustomDateTimeObjectInUtc( |
| 179 | $criteria['dates'][1] |
| 180 | )->modify('+1 day')->format('Y-m-d H:i:s'); |
| 181 | } |
| 182 | |
| 183 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 184 | |
| 185 | $limit = $this->getLimit( |
| 186 | !empty($criteria['page']) ? (int)$criteria['page'] : 0, |
| 187 | (int)$itemsPerPage |
| 188 | ); |
| 189 | |
| 190 | $usersTable = UsersTable::getTableName(); |
| 191 | |
| 192 | $statement = $this->connection->prepare( |
| 193 | "SELECT h.*, CONCAT(u.firstName, ' ', u.lastName) as userFullName |
| 194 | FROM {$this->table} h |
| 195 | LEFT JOIN {$usersTable} u ON h.userId = u.id |
| 196 | WHERE 1=1 $where |
| 197 | ORDER BY h.id DESC |
| 198 | {$limit}" |
| 199 | ); |
| 200 | |
| 201 | $statement->execute($params); |
| 202 | |
| 203 | $rows = $statement->fetchAll(); |
| 204 | |
| 205 | foreach ($rows as &$row) { |
| 206 | $row['dateTime'] = DateTimeService::getCustomDateTimeFromUtc($row['dateTime']); |
| 207 | } |
| 208 | } catch (\Exception $e) { |
| 209 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 210 | } |
| 211 | |
| 212 | return $rows; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @param $criteria |
| 217 | * |
| 218 | * @return mixed |
| 219 | * @throws QueryExecutionException |
| 220 | */ |
| 221 | public function getCount($criteria) |
| 222 | { |
| 223 | try { |
| 224 | $params = []; |
| 225 | $where = []; |
| 226 | |
| 227 | if (!empty($criteria['dates'])) { |
| 228 | $where[] = "(DATE_FORMAT(h.dateTime, '%Y-%m-%d %H:%i:%s') BETWEEN :dateFrom AND :dateTo)"; |
| 229 | $params[':dateFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 230 | $params[':dateTo'] = DateTimeService::getCustomDateTimeObjectInUtc( |
| 231 | $criteria['dates'][1] |
| 232 | )->modify('+1 day')->format('Y-m-d H:i:s'); |
| 233 | } |
| 234 | |
| 235 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 236 | |
| 237 | $statement = $this->connection->prepare( |
| 238 | "SELECT COUNT(*) AS count |
| 239 | FROM {$this->table} h |
| 240 | WHERE 1=1 {$where}" |
| 241 | ); |
| 242 | |
| 243 | $statement->execute($params); |
| 244 | |
| 245 | $row = $statement->fetch()['count']; |
| 246 | |
| 247 | } catch (\Exception $e) { |
| 248 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 249 | } |
| 250 | |
| 251 | return $row; |
| 252 | } |
| 253 | } |
| 254 |