api/Notifications.js

  1. const fetch = require('node-fetch');
  2. const Trello = require('../trello');
  3. const BASE_URL = 'https://api.trello.com/1/notifications';
  4. /**
  5. * Get notification by id.
  6. * @async
  7. * @function
  8. * @memberOf Trello
  9. * @param {string} idNotification
  10. * @param {Object} options - {board, board_fields, card, card_fields, display, entities, fields, list, member, member_fields, memberCreator, memberCreator_fields, organization, organization_fields}
  11. * @returns {Promise<*>}
  12. */
  13. Trello.prototype.getNotification = async function (idNotification, options) {
  14. const Defaults = {
  15. board: '',
  16. board_fields: '',
  17. card: '',
  18. card_fields: '',
  19. display: '',
  20. entities: '',
  21. fields: '',
  22. list: '',
  23. member: '',
  24. member_fields: '',
  25. memberCreator: '',
  26. memberCreator_fields: '',
  27. organization: '',
  28. organization_fields: '',
  29. }
  30. let url = `${BASE_URL}/${idNotification}?key=${this.key}&token=${this.token}`;
  31. options = Object.assign({}, Defaults, options);
  32. for (let key in options) {
  33. if (options[key] !== '') {
  34. url += `&${key}=${options[key]}`;
  35. }
  36. }
  37. const response = await fetch(url, {method: 'GET', headers: this.headers})
  38. const json = await response.json();
  39. return json;
  40. }
  41. /**
  42. * Update notification Read status by id.
  43. * @async
  44. * @function
  45. * @memberOf Trello
  46. * @param {string} idNotification
  47. * @param {Object} options - {unread}
  48. * @returns {Promise<*>}
  49. */
  50. Trello.prototype.updateNotificationReadStatus = async function (idNotification, options) {
  51. const Defaults = {
  52. unread: '',
  53. }
  54. let url = `${BASE_URL}/${idNotification}?key=${this.key}&token=${this.token}`;
  55. options = Object.assign({}, Defaults, options);
  56. for (let key in options) {
  57. if (options[key] !== '') {
  58. url += `&${key}=${options[key]}`;
  59. }
  60. }
  61. const response = await fetch(url, {method: 'PUT', headers: this.headers})
  62. const json = await response.json();
  63. return json;
  64. }
  65. /**
  66. * Get notification field by id.
  67. * @async
  68. * @function
  69. * @memberOf Trello
  70. * @param {string} idNotification
  71. * @param {string} field
  72. * @returns {Promise<*>}
  73. */
  74. Trello.prototype.getNotificationField = async function (idNotification, field) {
  75. const url = `${BASE_URL}/${idNotification}/${field}?key=${this.key}&token=${this.token}`;
  76. const response = await fetch(url, {method: 'GET', headers: this.headers})
  77. const json = await response.json();
  78. return json;
  79. }
  80. /**
  81. * Mark all notifications as read.
  82. * @async
  83. * @function
  84. * @memberOf Trello
  85. * @param {string} options - {read, ids}
  86. * @returns {Promise<*>}
  87. */
  88. Trello.prototype.markAllNotificationsAsRead = async function (options) {
  89. const Defaults = {
  90. read: '',
  91. ids: '',
  92. }
  93. let url = `${BASE_URL}/all/read?key=${this.key}&token=${this.token}`;
  94. options = Object.assign({}, Defaults, options);
  95. for (let key in options) {
  96. if (options[key] !== '') {
  97. url += `&${key}=${options[key]}`;
  98. }
  99. }
  100. const response = await fetch(url, {method: 'POST', headers: this.headers})
  101. const json = await response.json();
  102. return json;
  103. }
  104. /**
  105. * Update notifications read status.
  106. * @async
  107. * @function
  108. * @memberOf Trello
  109. * @param id
  110. * @param options - {value}
  111. * @returns {Promise<*>}
  112. */
  113. Trello.prototype.updateNotificationsReadStatus = async function (id, options){
  114. const Defaults = {
  115. value: '',
  116. }
  117. let url = `${BASE_URL}/${id}/unread?key=${this.key}&token=${this.token}`;
  118. options = Object.assign({}, Defaults, options);
  119. for (let key in options) {
  120. if (options[key] !== '') {
  121. url += `&${key}=${options[key]}`;
  122. }
  123. }
  124. const response = await fetch(url, {method: 'PUT', headers: this.headers})
  125. const json = await response.json();
  126. return json;
  127. }
  128. /**
  129. * Get notification board by id.
  130. * @async
  131. * @function
  132. * @memberOf Trello
  133. * @param {string} idNotification
  134. * @param {Object} options - {fields}
  135. * @returns {Promise<*>}
  136. */
  137. Trello.prototype.getNotificationBoard = async function (idNotification, options) {
  138. const Defaults = {
  139. fields: '',
  140. }
  141. let url = `${BASE_URL}/${idNotification}/board?key=${this.key}&token=${this.token}`;
  142. options = Object.assign({}, Defaults, options);
  143. for (let key in options) {
  144. if (options[key] !== '') {
  145. url += `&${key}=${options[key]}`;
  146. }
  147. }
  148. const response = await fetch(url, {method: 'GET', headers: this.headers})
  149. const json = await response.json();
  150. return json;
  151. }
  152. /**
  153. * Get notification card by id.
  154. * @async
  155. * @function
  156. * @memberOf Trello
  157. * @param {string} idNotification
  158. * @param {string} options - {fields}
  159. * @returns {Promise<*>}
  160. */
  161. Trello.prototype.getNotificationCard = async function (idNotification, options) {
  162. const Defaults = {
  163. fields: '',
  164. }
  165. let url = `${BASE_URL}/${idNotification}/card?key=${this.key}&token=${this.token}`;
  166. options = Object.assign({}, Defaults, options);
  167. for (let key in options) {
  168. if (options[key] !== '') {
  169. url += `&${key}=${options[key]}`;
  170. }
  171. }
  172. const response = await fetch(url, {method: 'GET', headers: this.headers})
  173. const json = await response.json();
  174. return json;
  175. }
  176. /**
  177. * Get notification list by id.
  178. * @async
  179. * @function
  180. * @memberOf Trello
  181. * @param {string} idNotification
  182. * @param {Object} options - {fields}
  183. * @returns {Promise<*>}
  184. */
  185. Trello.prototype.getNotificationList = async function (idNotification, options) {
  186. const Defaults = {
  187. fields: '',
  188. }
  189. let url = `${BASE_URL}/${idNotification}/list?key=${this.key}&token=${this.token}`;
  190. options = Object.assign({}, Defaults, options);
  191. for (let key in options) {
  192. if (options[key] !== '') {
  193. url += `&${key}=${options[key]}`;
  194. }
  195. }
  196. const response = await fetch(url, {method: 'GET', headers: this.headers})
  197. const json = await response.json();
  198. return json;
  199. }
  200. /**
  201. * Get notification member by id.
  202. * @async
  203. * @function
  204. * @memberOf Trello
  205. * @param {string} idNotification
  206. * @param {Object} options - {fields}
  207. * @returns {Promise<*>}
  208. */
  209. Trello.prototype.getNotificationMember = async function (idNotification, options) {
  210. const Defaults = {
  211. fields: '',
  212. }
  213. let url = `${BASE_URL}/${idNotification}/member?key=${this.key}&token=${this.token}`;
  214. options = Object.assign({}, Defaults, options);
  215. for (let key in options) {
  216. if (options[key] !== '') {
  217. url += `&${key}=${options[key]}`;
  218. }
  219. }
  220. const response = await fetch(url, {method: 'GET', headers: this.headers})
  221. const json = await response.json();
  222. return json;
  223. }
  224. /**
  225. * Get notification member creator by id.
  226. * @async
  227. * @function
  228. * @memberOf Trello
  229. * @param {string} idNotification
  230. * @param {Object} options - {fields}
  231. * @returns {Promise<*>}
  232. */
  233. Trello.prototype.getNotificationMemberCreator = async function (idNotification, options) {
  234. const Defaults = {
  235. fields: '',
  236. }
  237. let url = `${BASE_URL}/${idNotification}/memberCreator?key=${this.key}&token=${this.token}`;
  238. options = Object.assign({}, Defaults, options);
  239. for (let key in options) {
  240. if (options[key] !== '') {
  241. url += `&${key}=${options[key]}`;
  242. }
  243. }
  244. const response = await fetch(url, {method: 'GET', headers: this.headers})
  245. const json = await response.json();
  246. return json;
  247. }
  248. /**
  249. * Get notification organization by id.
  250. * @async
  251. * @function
  252. * @memberOf Trello
  253. * @param {string} idNotification
  254. * @param {Object} options - {fields}
  255. * @returns {Promise<*>}
  256. */
  257. Trello.prototype.getNotificationOrganization = async function (idNotification, options) {
  258. const Defaults = {
  259. fields: '',
  260. }
  261. let url = `${BASE_URL}/${idNotification}/organization?key=${this.key}&token=${this.token}`;
  262. options = Object.assign({}, Defaults, options);
  263. for (let key in options) {
  264. if (options[key] !== '') {
  265. url += `&${key}=${options[key]}`;
  266. }
  267. }
  268. const response = await fetch(url, {method: 'GET', headers: this.headers})
  269. const json = await response.json();
  270. return json;
  271. }