ameliabooking
/
src
/
Infrastructure
/
Repository
/
Notification
/
NotificationSMSHistoryRepository.php
NotificationLogRepository.php
3 months ago
NotificationRepository.php
3 months ago
NotificationSMSHistoryRepository.php
3 months ago
NotificationsToEntitiesRepository.php
3 months ago
NotificationSMSHistoryRepository.php
246 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 int |
| 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 | $statement->execute($params); |
| 65 | |
| 66 | return $this->connection->lastInsertId(); |
| 67 | } catch (\Exception $e) { |
| 68 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param $id |
| 74 | * @param $data |
| 75 | * |
| 76 | * @return bool |
| 77 | * @throws QueryExecutionException |
| 78 | */ |
| 79 | public function update($id, $data) |
| 80 | { |
| 81 | $params = [ |
| 82 | ':status' => $data['status'], |
| 83 | ':price' => $data['price'], |
| 84 | ':id' => $id |
| 85 | ]; |
| 86 | |
| 87 | $sqlUpdate = ''; |
| 88 | |
| 89 | if (isset($data['logId'])) { |
| 90 | $params[':logId'] = $data['logId']; |
| 91 | |
| 92 | $sqlUpdate .= '`logId` = COALESCE(:logId, `logId`),'; |
| 93 | } |
| 94 | |
| 95 | if (isset($data['dateTime'])) { |
| 96 | $params[':dateTime'] = $data['dateTime']; |
| 97 | |
| 98 | $sqlUpdate .= '`dateTime` = COALESCE(:dateTime, `dateTime`),'; |
| 99 | } |
| 100 | |
| 101 | if (isset($data['segments'])) { |
| 102 | $params[':segments'] = $data['segments']; |
| 103 | |
| 104 | $sqlUpdate .= '`segments` = COALESCE(:segments, `segments`),'; |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | $statement = $this->connection->prepare( |
| 109 | "UPDATE {$this->table} SET |
| 110 | {$sqlUpdate} |
| 111 | `status` = COALESCE(:status, `status`), |
| 112 | `price` = COALESCE(:price, `price`) |
| 113 | WHERE id = :id" |
| 114 | ); |
| 115 | |
| 116 | $statement->execute($params); |
| 117 | |
| 118 | return true; |
| 119 | } catch (\Exception $e) { |
| 120 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param int $id |
| 126 | * |
| 127 | * @return array|null |
| 128 | * @throws QueryExecutionException |
| 129 | */ |
| 130 | public function getItemById($id) |
| 131 | { |
| 132 | try { |
| 133 | $statement = $this->connection->prepare( |
| 134 | $this->selectQuery() . " WHERE id = :id" |
| 135 | ); |
| 136 | |
| 137 | $params = [ |
| 138 | ':id' => $id, |
| 139 | ]; |
| 140 | |
| 141 | $statement->execute($params); |
| 142 | |
| 143 | $row = $statement->fetch(); |
| 144 | } catch (\Exception $e) { |
| 145 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 146 | } |
| 147 | |
| 148 | if (!$row) { |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | return $row; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param $criteria |
| 157 | * @param $itemsPerPage |
| 158 | * |
| 159 | * @return array |
| 160 | * @throws QueryExecutionException |
| 161 | */ |
| 162 | public function getFiltered($criteria, $itemsPerPage) |
| 163 | { |
| 164 | try { |
| 165 | $params = []; |
| 166 | $where = []; |
| 167 | |
| 168 | if (!empty($criteria['dates'])) { |
| 169 | $where[] = "(h.dateTime BETWEEN :dateFrom AND :dateTo)"; |
| 170 | $params[':dateFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 171 | $params[':dateTo'] = DateTimeService::getCustomDateTimeObjectInUtc( |
| 172 | $criteria['dates'][1] |
| 173 | )->modify('+1 day')->format('Y-m-d H:i:s'); |
| 174 | } |
| 175 | |
| 176 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 177 | |
| 178 | $limit = $this->getLimit( |
| 179 | !empty($criteria['page']) ? (int)$criteria['page'] : 0, |
| 180 | (int)$itemsPerPage |
| 181 | ); |
| 182 | |
| 183 | $usersTable = UsersTable::getTableName(); |
| 184 | |
| 185 | $statement = $this->connection->prepare( |
| 186 | "SELECT h.*, CONCAT(u.firstName, ' ', u.lastName) as userFullName |
| 187 | FROM {$this->table} h |
| 188 | LEFT JOIN {$usersTable} u ON h.userId = u.id |
| 189 | WHERE 1=1 $where |
| 190 | ORDER BY h.id DESC |
| 191 | {$limit}" |
| 192 | ); |
| 193 | |
| 194 | $statement->execute($params); |
| 195 | |
| 196 | $rows = $statement->fetchAll(); |
| 197 | |
| 198 | foreach ($rows as &$row) { |
| 199 | $row['dateTime'] = DateTimeService::getCustomDateTimeFromUtc($row['dateTime']); |
| 200 | } |
| 201 | } catch (\Exception $e) { |
| 202 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 203 | } |
| 204 | |
| 205 | return $rows; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param $criteria |
| 210 | * |
| 211 | * @return mixed |
| 212 | * @throws QueryExecutionException |
| 213 | */ |
| 214 | public function getCount($criteria) |
| 215 | { |
| 216 | try { |
| 217 | $params = []; |
| 218 | $where = []; |
| 219 | |
| 220 | if (!empty($criteria['dates'])) { |
| 221 | $where[] = "(h.dateTime BETWEEN :dateFrom AND :dateTo)"; |
| 222 | $params[':dateFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 223 | $params[':dateTo'] = DateTimeService::getCustomDateTimeObjectInUtc( |
| 224 | $criteria['dates'][1] |
| 225 | )->modify('+1 day')->format('Y-m-d H:i:s'); |
| 226 | } |
| 227 | |
| 228 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 229 | |
| 230 | $statement = $this->connection->prepare( |
| 231 | "SELECT COUNT(*) AS count |
| 232 | FROM {$this->table} h |
| 233 | WHERE 1=1 {$where}" |
| 234 | ); |
| 235 | |
| 236 | $statement->execute($params); |
| 237 | |
| 238 | $row = $statement->fetch()['count']; |
| 239 | } catch (\Exception $e) { |
| 240 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 241 | } |
| 242 | |
| 243 | return $row; |
| 244 | } |
| 245 | } |
| 246 |