NEW: Create thirdparty notification by action code (#30541)

* based on createCompanyNotification, just trying to do it with code and not id number

* can now create notifications by action trigger code

* will now prevent duplications

* checks if request_data contains event or fk_action key because then it uses that and not the action code supplied as text

* checks if request_data contains event or fk_action key because then it uses that and not the action code supplied as text

* now requires a well formed request_data with an event key

---------

Co-authored-by: Jon Bendtsen <xcodeauthor@jonb.dk>
This commit is contained in:
Jon Bendtsen 2024-08-11 00:20:45 +02:00 committed by GitHub
parent 3805b61408
commit 8a35651b3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1199,6 +1199,24 @@ class Thirdparties extends DolibarrApi
$notification->$field = $value;
}
$event = $notification->event;
if (!$event) {
throw new RestException(500, 'Error creating Thirdparty Notification, request_data missing event');
}
$socid = $notification->socid;
$contact_id = $notification->contact_id;
$exists_sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
$exists_sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
$exists_sql .= " WHERE fk_action = '".$this->db->escape($event)."'";
$exists_sql .= " AND fk_soc = '".$this->db->escape($socid)."'";
$exists_sql .= " AND fk_contact = '".$this->db->escape($contact_id)."'";
$exists_result = $this->db->query($exists_sql);
if ($this->db->num_rows($exists_sql) > 0) {
throw new RestException(403, 'Notification already exists');
}
if ($notification->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, 'Error creating Thirdparty Notification');
}
@ -1210,6 +1228,72 @@ class Thirdparties extends DolibarrApi
return $this->_cleanObjectDatas($notification);
}
/**
* Create CompanyNotification object for thirdparty using action trigger code
* @param int $id ID of thirdparty
* @param string $code Action Trigger code
* @param array $request_data Request data
*
* @return array|mixed Notification of thirdparty
*
* @url POST {id}/notificationsbycode/{code}
*/
public function createCompanyNotificationByCode($id, $code, $request_data = null)
{
if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
throw new RestException(403, "User has no right to update thirdparties");
}
if ($this->company->fetch($id) <= 0) {
throw new RestException(404, 'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
}
$notification = new Notify($this->db);
$notification->socid = $id;
$sql = "SELECT t.rowid as id FROM ".MAIN_DB_PREFIX."c_action_trigger as t";
$sql .= " WHERE t.code = '".$this->db->escape($code)."'";
$result = $this->db->query($sql);
if ($this->db->num_rows($result) == 0) {
throw new RestException(404, 'Action Trigger code not found');
}
$notification->event = $this->db->fetch_row($result)[0];
foreach ($request_data as $field => $value) {
if ($field === 'event') {
throw new RestException(500, 'Error creating Thirdparty Notification, request_data contains event key');
}
if ($field === 'fk_action') {
throw new RestException(500, 'Error creating Thirdparty Notification, request_data contains fk_action key');
}
$notification->$field = $value;
}
$event = $notification->event;
$socid = $notification->socid;
$contact_id = $notification->contact_id;
$exists_sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
$exists_sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
$exists_sql .= " WHERE fk_action = '".$this->db->escape($event)."'";
$exists_sql .= " AND fk_soc = '".$this->db->escape($socid)."'";
$exists_sql .= " AND fk_contact = '".$this->db->escape($contact_id)."'";
$exists_result = $this->db->query($exists_sql);
if ($this->db->num_rows($exists_sql) > 0) {
throw new RestException(403, 'Notification already exists');
}
if ($notification->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, 'Error creating Thirdparty Notification, are request_data well formed?');
}
if ($notification->update(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, 'Error updating values');
}
return $this->_cleanObjectDatas($notification);
}
/**
* Delete a CompanyNotification attached to a thirdparty
*