user.js.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>Code coverage report for nodejs-homework-API/controllers/user.js</title>
  5. <meta charset="utf-8" />
  6. <link rel="stylesheet" href="../../prettify.css" />
  7. <link rel="stylesheet" href="../../base.css" />
  8. <link rel="shortcut icon" type="image/x-icon" href="../../favicon.png" />
  9. <meta name="viewport" content="width=device-width, initial-scale=1" />
  10. <style type='text/css'>
  11. .coverage-summary .sorter {
  12. background-image: url(../../sort-arrow-sprite.png);
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class='wrapper'>
  18. <div class='pad1'>
  19. <h1><a href="../../index.html">All files</a> / <a href="index.html">nodejs-homework-API/controllers</a> user.js</h1>
  20. <div class='clearfix'>
  21. <div class='fl pad1y space-right2'>
  22. <span class="strong">23.08% </span>
  23. <span class="quiet">Statements</span>
  24. <span class='fraction'>15/65</span>
  25. </div>
  26. <div class='fl pad1y space-right2'>
  27. <span class="strong">0% </span>
  28. <span class="quiet">Branches</span>
  29. <span class='fraction'>0/10</span>
  30. </div>
  31. <div class='fl pad1y space-right2'>
  32. <span class="strong">0% </span>
  33. <span class="quiet">Functions</span>
  34. <span class='fraction'>0/6</span>
  35. </div>
  36. <div class='fl pad1y space-right2'>
  37. <span class="strong">23.08% </span>
  38. <span class="quiet">Lines</span>
  39. <span class='fraction'>15/65</span>
  40. </div>
  41. </div>
  42. <p class="quiet">
  43. Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
  44. </p>
  45. </div>
  46. <div class='status-line low'></div>
  47. <pre><table class="coverage">
  48. <tr><td class="line-count quiet"><a name='L1'></a><a href='#L1'>1</a>
  49. <a name='L2'></a><a href='#L2'>2</a>
  50. <a name='L3'></a><a href='#L3'>3</a>
  51. <a name='L4'></a><a href='#L4'>4</a>
  52. <a name='L5'></a><a href='#L5'>5</a>
  53. <a name='L6'></a><a href='#L6'>6</a>
  54. <a name='L7'></a><a href='#L7'>7</a>
  55. <a name='L8'></a><a href='#L8'>8</a>
  56. <a name='L9'></a><a href='#L9'>9</a>
  57. <a name='L10'></a><a href='#L10'>10</a>
  58. <a name='L11'></a><a href='#L11'>11</a>
  59. <a name='L12'></a><a href='#L12'>12</a>
  60. <a name='L13'></a><a href='#L13'>13</a>
  61. <a name='L14'></a><a href='#L14'>14</a>
  62. <a name='L15'></a><a href='#L15'>15</a>
  63. <a name='L16'></a><a href='#L16'>16</a>
  64. <a name='L17'></a><a href='#L17'>17</a>
  65. <a name='L18'></a><a href='#L18'>18</a>
  66. <a name='L19'></a><a href='#L19'>19</a>
  67. <a name='L20'></a><a href='#L20'>20</a>
  68. <a name='L21'></a><a href='#L21'>21</a>
  69. <a name='L22'></a><a href='#L22'>22</a>
  70. <a name='L23'></a><a href='#L23'>23</a>
  71. <a name='L24'></a><a href='#L24'>24</a>
  72. <a name='L25'></a><a href='#L25'>25</a>
  73. <a name='L26'></a><a href='#L26'>26</a>
  74. <a name='L27'></a><a href='#L27'>27</a>
  75. <a name='L28'></a><a href='#L28'>28</a>
  76. <a name='L29'></a><a href='#L29'>29</a>
  77. <a name='L30'></a><a href='#L30'>30</a>
  78. <a name='L31'></a><a href='#L31'>31</a>
  79. <a name='L32'></a><a href='#L32'>32</a>
  80. <a name='L33'></a><a href='#L33'>33</a>
  81. <a name='L34'></a><a href='#L34'>34</a>
  82. <a name='L35'></a><a href='#L35'>35</a>
  83. <a name='L36'></a><a href='#L36'>36</a>
  84. <a name='L37'></a><a href='#L37'>37</a>
  85. <a name='L38'></a><a href='#L38'>38</a>
  86. <a name='L39'></a><a href='#L39'>39</a>
  87. <a name='L40'></a><a href='#L40'>40</a>
  88. <a name='L41'></a><a href='#L41'>41</a>
  89. <a name='L42'></a><a href='#L42'>42</a>
  90. <a name='L43'></a><a href='#L43'>43</a>
  91. <a name='L44'></a><a href='#L44'>44</a>
  92. <a name='L45'></a><a href='#L45'>45</a>
  93. <a name='L46'></a><a href='#L46'>46</a>
  94. <a name='L47'></a><a href='#L47'>47</a>
  95. <a name='L48'></a><a href='#L48'>48</a>
  96. <a name='L49'></a><a href='#L49'>49</a>
  97. <a name='L50'></a><a href='#L50'>50</a>
  98. <a name='L51'></a><a href='#L51'>51</a>
  99. <a name='L52'></a><a href='#L52'>52</a>
  100. <a name='L53'></a><a href='#L53'>53</a>
  101. <a name='L54'></a><a href='#L54'>54</a>
  102. <a name='L55'></a><a href='#L55'>55</a>
  103. <a name='L56'></a><a href='#L56'>56</a>
  104. <a name='L57'></a><a href='#L57'>57</a>
  105. <a name='L58'></a><a href='#L58'>58</a>
  106. <a name='L59'></a><a href='#L59'>59</a>
  107. <a name='L60'></a><a href='#L60'>60</a>
  108. <a name='L61'></a><a href='#L61'>61</a>
  109. <a name='L62'></a><a href='#L62'>62</a>
  110. <a name='L63'></a><a href='#L63'>63</a>
  111. <a name='L64'></a><a href='#L64'>64</a>
  112. <a name='L65'></a><a href='#L65'>65</a>
  113. <a name='L66'></a><a href='#L66'>66</a>
  114. <a name='L67'></a><a href='#L67'>67</a>
  115. <a name='L68'></a><a href='#L68'>68</a>
  116. <a name='L69'></a><a href='#L69'>69</a>
  117. <a name='L70'></a><a href='#L70'>70</a>
  118. <a name='L71'></a><a href='#L71'>71</a>
  119. <a name='L72'></a><a href='#L72'>72</a>
  120. <a name='L73'></a><a href='#L73'>73</a>
  121. <a name='L74'></a><a href='#L74'>74</a>
  122. <a name='L75'></a><a href='#L75'>75</a>
  123. <a name='L76'></a><a href='#L76'>76</a>
  124. <a name='L77'></a><a href='#L77'>77</a>
  125. <a name='L78'></a><a href='#L78'>78</a>
  126. <a name='L79'></a><a href='#L79'>79</a>
  127. <a name='L80'></a><a href='#L80'>80</a>
  128. <a name='L81'></a><a href='#L81'>81</a>
  129. <a name='L82'></a><a href='#L82'>82</a>
  130. <a name='L83'></a><a href='#L83'>83</a>
  131. <a name='L84'></a><a href='#L84'>84</a>
  132. <a name='L85'></a><a href='#L85'>85</a>
  133. <a name='L86'></a><a href='#L86'>86</a>
  134. <a name='L87'></a><a href='#L87'>87</a>
  135. <a name='L88'></a><a href='#L88'>88</a>
  136. <a name='L89'></a><a href='#L89'>89</a>
  137. <a name='L90'></a><a href='#L90'>90</a>
  138. <a name='L91'></a><a href='#L91'>91</a>
  139. <a name='L92'></a><a href='#L92'>92</a>
  140. <a name='L93'></a><a href='#L93'>93</a>
  141. <a name='L94'></a><a href='#L94'>94</a>
  142. <a name='L95'></a><a href='#L95'>95</a>
  143. <a name='L96'></a><a href='#L96'>96</a>
  144. <a name='L97'></a><a href='#L97'>97</a>
  145. <a name='L98'></a><a href='#L98'>98</a>
  146. <a name='L99'></a><a href='#L99'>99</a>
  147. <a name='L100'></a><a href='#L100'>100</a>
  148. <a name='L101'></a><a href='#L101'>101</a>
  149. <a name='L102'></a><a href='#L102'>102</a>
  150. <a name='L103'></a><a href='#L103'>103</a>
  151. <a name='L104'></a><a href='#L104'>104</a>
  152. <a name='L105'></a><a href='#L105'>105</a>
  153. <a name='L106'></a><a href='#L106'>106</a>
  154. <a name='L107'></a><a href='#L107'>107</a>
  155. <a name='L108'></a><a href='#L108'>108</a>
  156. <a name='L109'></a><a href='#L109'>109</a>
  157. <a name='L110'></a><a href='#L110'>110</a>
  158. <a name='L111'></a><a href='#L111'>111</a>
  159. <a name='L112'></a><a href='#L112'>112</a>
  160. <a name='L113'></a><a href='#L113'>113</a>
  161. <a name='L114'></a><a href='#L114'>114</a>
  162. <a name='L115'></a><a href='#L115'>115</a>
  163. <a name='L116'></a><a href='#L116'>116</a>
  164. <a name='L117'></a><a href='#L117'>117</a>
  165. <a name='L118'></a><a href='#L118'>118</a>
  166. <a name='L119'></a><a href='#L119'>119</a>
  167. <a name='L120'></a><a href='#L120'>120</a>
  168. <a name='L121'></a><a href='#L121'>121</a>
  169. <a name='L122'></a><a href='#L122'>122</a>
  170. <a name='L123'></a><a href='#L123'>123</a>
  171. <a name='L124'></a><a href='#L124'>124</a>
  172. <a name='L125'></a><a href='#L125'>125</a>
  173. <a name='L126'></a><a href='#L126'>126</a>
  174. <a name='L127'></a><a href='#L127'>127</a>
  175. <a name='L128'></a><a href='#L128'>128</a>
  176. <a name='L129'></a><a href='#L129'>129</a>
  177. <a name='L130'></a><a href='#L130'>130</a>
  178. <a name='L131'></a><a href='#L131'>131</a>
  179. <a name='L132'></a><a href='#L132'>132</a>
  180. <a name='L133'></a><a href='#L133'>133</a>
  181. <a name='L134'></a><a href='#L134'>134</a>
  182. <a name='L135'></a><a href='#L135'>135</a>
  183. <a name='L136'></a><a href='#L136'>136</a>
  184. <a name='L137'></a><a href='#L137'>137</a>
  185. <a name='L138'></a><a href='#L138'>138</a>
  186. <a name='L139'></a><a href='#L139'>139</a>
  187. <a name='L140'></a><a href='#L140'>140</a>
  188. <a name='L141'></a><a href='#L141'>141</a>
  189. <a name='L142'></a><a href='#L142'>142</a>
  190. <a name='L143'></a><a href='#L143'>143</a>
  191. <a name='L144'></a><a href='#L144'>144</a>
  192. <a name='L145'></a><a href='#L145'>145</a>
  193. <a name='L146'></a><a href='#L146'>146</a>
  194. <a name='L147'></a><a href='#L147'>147</a>
  195. <a name='L148'></a><a href='#L148'>148</a>
  196. <a name='L149'></a><a href='#L149'>149</a>
  197. <a name='L150'></a><a href='#L150'>150</a>
  198. <a name='L151'></a><a href='#L151'>151</a>
  199. <a name='L152'></a><a href='#L152'>152</a>
  200. <a name='L153'></a><a href='#L153'>153</a>
  201. <a name='L154'></a><a href='#L154'>154</a>
  202. <a name='L155'></a><a href='#L155'>155</a></td><td class="line-coverage quiet"><span class="cline-any cline-yes">1x</span>
  203. <span class="cline-any cline-yes">1x</span>
  204. <span class="cline-any cline-yes">1x</span>
  205. <span class="cline-any cline-yes">1x</span>
  206. <span class="cline-any cline-yes">1x</span>
  207. <span class="cline-any cline-yes">1x</span>
  208. <span class="cline-any cline-yes">1x</span>
  209. <span class="cline-any cline-yes">1x</span>
  210. <span class="cline-any cline-neutral">&nbsp;</span>
  211. <span class="cline-any cline-yes">1x</span>
  212. <span class="cline-any cline-no">&nbsp;</span>
  213. <span class="cline-any cline-no">&nbsp;</span>
  214. <span class="cline-any cline-no">&nbsp;</span>
  215. <span class="cline-any cline-no">&nbsp;</span>
  216. <span class="cline-any cline-no">&nbsp;</span>
  217. <span class="cline-any cline-neutral">&nbsp;</span>
  218. <span class="cline-any cline-no">&nbsp;</span>
  219. <span class="cline-any cline-no">&nbsp;</span>
  220. <span class="cline-any cline-neutral">&nbsp;</span>
  221. <span class="cline-any cline-neutral">&nbsp;</span>
  222. <span class="cline-any cline-neutral">&nbsp;</span>
  223. <span class="cline-any cline-neutral">&nbsp;</span>
  224. <span class="cline-any cline-neutral">&nbsp;</span>
  225. <span class="cline-any cline-neutral">&nbsp;</span>
  226. <span class="cline-any cline-neutral">&nbsp;</span>
  227. <span class="cline-any cline-neutral">&nbsp;</span>
  228. <span class="cline-any cline-no">&nbsp;</span>
  229. <span class="cline-any cline-no">&nbsp;</span>
  230. <span class="cline-any cline-no">&nbsp;</span>
  231. <span class="cline-any cline-no">&nbsp;</span>
  232. <span class="cline-any cline-no">&nbsp;</span>
  233. <span class="cline-any cline-no">&nbsp;</span>
  234. <span class="cline-any cline-neutral">&nbsp;</span>
  235. <span class="cline-any cline-neutral">&nbsp;</span>
  236. <span class="cline-any cline-neutral">&nbsp;</span>
  237. <span class="cline-any cline-neutral">&nbsp;</span>
  238. <span class="cline-any cline-neutral">&nbsp;</span>
  239. <span class="cline-any cline-neutral">&nbsp;</span>
  240. <span class="cline-any cline-neutral">&nbsp;</span>
  241. <span class="cline-any cline-no">&nbsp;</span>
  242. <span class="cline-any cline-neutral">&nbsp;</span>
  243. <span class="cline-any cline-neutral">&nbsp;</span>
  244. <span class="cline-any cline-neutral">&nbsp;</span>
  245. <span class="cline-any cline-yes">1x</span>
  246. <span class="cline-any cline-no">&nbsp;</span>
  247. <span class="cline-any cline-no">&nbsp;</span>
  248. <span class="cline-any cline-no">&nbsp;</span>
  249. <span class="cline-any cline-no">&nbsp;</span>
  250. <span class="cline-any cline-no">&nbsp;</span>
  251. <span class="cline-any cline-neutral">&nbsp;</span>
  252. <span class="cline-any cline-neutral">&nbsp;</span>
  253. <span class="cline-any cline-neutral">&nbsp;</span>
  254. <span class="cline-any cline-neutral">&nbsp;</span>
  255. <span class="cline-any cline-neutral">&nbsp;</span>
  256. <span class="cline-any cline-neutral">&nbsp;</span>
  257. <span class="cline-any cline-no">&nbsp;</span>
  258. <span class="cline-any cline-no">&nbsp;</span>
  259. <span class="cline-any cline-neutral">&nbsp;</span>
  260. <span class="cline-any cline-neutral">&nbsp;</span>
  261. <span class="cline-any cline-neutral">&nbsp;</span>
  262. <span class="cline-any cline-neutral">&nbsp;</span>
  263. <span class="cline-any cline-neutral">&nbsp;</span>
  264. <span class="cline-any cline-neutral">&nbsp;</span>
  265. <span class="cline-any cline-neutral">&nbsp;</span>
  266. <span class="cline-any cline-neutral">&nbsp;</span>
  267. <span class="cline-any cline-neutral">&nbsp;</span>
  268. <span class="cline-any cline-neutral">&nbsp;</span>
  269. <span class="cline-any cline-no">&nbsp;</span>
  270. <span class="cline-any cline-neutral">&nbsp;</span>
  271. <span class="cline-any cline-neutral">&nbsp;</span>
  272. <span class="cline-any cline-neutral">&nbsp;</span>
  273. <span class="cline-any cline-yes">1x</span>
  274. <span class="cline-any cline-no">&nbsp;</span>
  275. <span class="cline-any cline-no">&nbsp;</span>
  276. <span class="cline-any cline-no">&nbsp;</span>
  277. <span class="cline-any cline-no">&nbsp;</span>
  278. <span class="cline-any cline-no">&nbsp;</span>
  279. <span class="cline-any cline-no">&nbsp;</span>
  280. <span class="cline-any cline-neutral">&nbsp;</span>
  281. <span class="cline-any cline-neutral">&nbsp;</span>
  282. <span class="cline-any cline-neutral">&nbsp;</span>
  283. <span class="cline-any cline-neutral">&nbsp;</span>
  284. <span class="cline-any cline-neutral">&nbsp;</span>
  285. <span class="cline-any cline-neutral">&nbsp;</span>
  286. <span class="cline-any cline-no">&nbsp;</span>
  287. <span class="cline-any cline-no">&nbsp;</span>
  288. <span class="cline-any cline-no">&nbsp;</span>
  289. <span class="cline-any cline-no">&nbsp;</span>
  290. <span class="cline-any cline-no">&nbsp;</span>
  291. <span class="cline-any cline-neutral">&nbsp;</span>
  292. <span class="cline-any cline-neutral">&nbsp;</span>
  293. <span class="cline-any cline-neutral">&nbsp;</span>
  294. <span class="cline-any cline-neutral">&nbsp;</span>
  295. <span class="cline-any cline-neutral">&nbsp;</span>
  296. <span class="cline-any cline-neutral">&nbsp;</span>
  297. <span class="cline-any cline-neutral">&nbsp;</span>
  298. <span class="cline-any cline-no">&nbsp;</span>
  299. <span class="cline-any cline-neutral">&nbsp;</span>
  300. <span class="cline-any cline-neutral">&nbsp;</span>
  301. <span class="cline-any cline-neutral">&nbsp;</span>
  302. <span class="cline-any cline-yes">1x</span>
  303. <span class="cline-any cline-no">&nbsp;</span>
  304. <span class="cline-any cline-no">&nbsp;</span>
  305. <span class="cline-any cline-no">&nbsp;</span>
  306. <span class="cline-any cline-no">&nbsp;</span>
  307. <span class="cline-any cline-neutral">&nbsp;</span>
  308. <span class="cline-any cline-neutral">&nbsp;</span>
  309. <span class="cline-any cline-neutral">&nbsp;</span>
  310. <span class="cline-any cline-neutral">&nbsp;</span>
  311. <span class="cline-any cline-neutral">&nbsp;</span>
  312. <span class="cline-any cline-neutral">&nbsp;</span>
  313. <span class="cline-any cline-no">&nbsp;</span>
  314. <span class="cline-any cline-no">&nbsp;</span>
  315. <span class="cline-any cline-neutral">&nbsp;</span>
  316. <span class="cline-any cline-yes">1x</span>
  317. <span class="cline-any cline-no">&nbsp;</span>
  318. <span class="cline-any cline-no">&nbsp;</span>
  319. <span class="cline-any cline-no">&nbsp;</span>
  320. <span class="cline-any cline-neutral">&nbsp;</span>
  321. <span class="cline-any cline-neutral">&nbsp;</span>
  322. <span class="cline-any cline-neutral">&nbsp;</span>
  323. <span class="cline-any cline-neutral">&nbsp;</span>
  324. <span class="cline-any cline-neutral">&nbsp;</span>
  325. <span class="cline-any cline-neutral">&nbsp;</span>
  326. <span class="cline-any cline-neutral">&nbsp;</span>
  327. <span class="cline-any cline-no">&nbsp;</span>
  328. <span class="cline-any cline-neutral">&nbsp;</span>
  329. <span class="cline-any cline-neutral">&nbsp;</span>
  330. <span class="cline-any cline-neutral">&nbsp;</span>
  331. <span class="cline-any cline-neutral">&nbsp;</span>
  332. <span class="cline-any cline-neutral">&nbsp;</span>
  333. <span class="cline-any cline-neutral">&nbsp;</span>
  334. <span class="cline-any cline-neutral">&nbsp;</span>
  335. <span class="cline-any cline-yes">1x</span>
  336. <span class="cline-any cline-no">&nbsp;</span>
  337. <span class="cline-any cline-no">&nbsp;</span>
  338. <span class="cline-any cline-no">&nbsp;</span>
  339. <span class="cline-any cline-no">&nbsp;</span>
  340. <span class="cline-any cline-no">&nbsp;</span>
  341. <span class="cline-any cline-neutral">&nbsp;</span>
  342. <span class="cline-any cline-neutral">&nbsp;</span>
  343. <span class="cline-any cline-neutral">&nbsp;</span>
  344. <span class="cline-any cline-no">&nbsp;</span>
  345. <span class="cline-any cline-neutral">&nbsp;</span>
  346. <span class="cline-any cline-neutral">&nbsp;</span>
  347. <span class="cline-any cline-neutral">&nbsp;</span>
  348. <span class="cline-any cline-yes">1x</span>
  349. <span class="cline-any cline-neutral">&nbsp;</span>
  350. <span class="cline-any cline-neutral">&nbsp;</span>
  351. <span class="cline-any cline-neutral">&nbsp;</span>
  352. <span class="cline-any cline-neutral">&nbsp;</span>
  353. <span class="cline-any cline-neutral">&nbsp;</span>
  354. <span class="cline-any cline-neutral">&nbsp;</span>
  355. <span class="cline-any cline-neutral">&nbsp;</span>
  356. <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">const UserModel = require("../model/user");
  357. const fs = require("fs").promises;
  358. const path = require("path");
  359. const Jimp = require("jimp");
  360. const jwt = require("jsonwebtoken");
  361. const createFolderIsExist = require("../helpers/create-directory");
  362. require("dotenv").config();
  363. const SECRET_KEY = process.env.JWT_SECRET;
  364. &nbsp;
  365. const saveAvatarForStatic = <span class="fstat-no" title="function not covered" >as</span>ync (req, res, next) =&gt; {
  366. <span class="cstat-no" title="statement not covered" > try {</span>
  367. const id = <span class="cstat-no" title="statement not covered" >req.user.id;</span>
  368. const DIR_IMAGES = <span class="cstat-no" title="statement not covered" >process.env.DIR_IMAGES;</span>
  369. const pathToFile = <span class="cstat-no" title="statement not covered" >req.file.path;</span>
  370. const newNameAvatar = <span class="cstat-no" title="statement not covered" >req.file.originalname;</span>
  371. &nbsp;
  372. const img = <span class="cstat-no" title="statement not covered" >await Jimp.read(pathToFile);</span>
  373. <span class="cstat-no" title="statement not covered" > await img</span>
  374. .autocrop()
  375. .cover(
  376. 250,
  377. 250,
  378. Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE
  379. )
  380. .writeAsync(pathToFile);
  381. &nbsp;
  382. <span class="cstat-no" title="statement not covered" > await createFolderIsExist(path.join(DIR_IMAGES, id));</span>
  383. <span class="cstat-no" title="statement not covered" > await fs.rename(pathToFile, path.join(DIR_IMAGES, id, newNameAvatar));</span>
  384. const newUrlAvatar = <span class="cstat-no" title="statement not covered" >path.normalize(path.join(id, newNameAvatar));</span>
  385. const newUrl = <span class="cstat-no" title="statement not covered" >`http://localhost:3000/${id}/${newNameAvatar}`;</span>
  386. <span class="cstat-no" title="statement not covered" > await UserModel.updateAvatar(id, newUrlAvatar);</span>
  387. <span class="cstat-no" title="statement not covered" > return res.status(200).json({</span>
  388. status: "success",
  389. code: 200,
  390. data: {
  391. newUrl,
  392. },
  393. });
  394. } catch (e) {
  395. <span class="cstat-no" title="statement not covered" > next(e);</span>
  396. }
  397. };
  398. &nbsp;
  399. const createNewUser = <span class="fstat-no" title="function not covered" >as</span>ync (req, res, next) =&gt; {
  400. <span class="cstat-no" title="statement not covered" > try {</span>
  401. const { email } = <span class="cstat-no" title="statement not covered" >req.body;</span>
  402. const isExist = <span class="cstat-no" title="statement not covered" >await UserModel.findByEmail(email);</span>
  403. <span class="cstat-no" title="statement not covered" > if (isExist) {</span>
  404. <span class="cstat-no" title="statement not covered" > return res.status(409).json({</span>
  405. status: "error",
  406. code: 409,
  407. data: "Conflict",
  408. message: "Email has used already!",
  409. });
  410. }
  411. const newUser = <span class="cstat-no" title="statement not covered" >await UserModel.createUser(req.body);</span>
  412. <span class="cstat-no" title="statement not covered" > return res.status(201).json({</span>
  413. status: "success",
  414. code: 201,
  415. data: {
  416. id: newUser.id,
  417. email: newUser.email,
  418. name: newUser.name,
  419. avatar: newUser.avatar,
  420. },
  421. });
  422. } catch (e) {
  423. <span class="cstat-no" title="statement not covered" > next(e);</span>
  424. }
  425. };
  426. &nbsp;
  427. const logIn = <span class="fstat-no" title="function not covered" >as</span>ync (req, res, next) =&gt; {
  428. <span class="cstat-no" title="statement not covered" > try {</span>
  429. const { email, password } = <span class="cstat-no" title="statement not covered" >req.body;</span>
  430. const user = <span class="cstat-no" title="statement not covered" >await UserModel.findByEmail(email);</span>
  431. const isValidPassword = <span class="cstat-no" title="statement not covered" >await user.validPassword(password);</span>
  432. <span class="cstat-no" title="statement not covered" > if (!user || !isValidPassword) {</span>
  433. <span class="cstat-no" title="statement not covered" > return res.status(401).json({</span>
  434. status: "error",
  435. code: 401,
  436. data: "UNAUTHORIZED",
  437. message: "Invalid credentials",
  438. });
  439. }
  440. const id = <span class="cstat-no" title="statement not covered" >user._id;</span>
  441. const payload = <span class="cstat-no" title="statement not covered" >{ id };</span>
  442. const token = <span class="cstat-no" title="statement not covered" >jwt.sign(payload, SECRET_KEY, { expiresIn: "2h" });</span>
  443. <span class="cstat-no" title="statement not covered" > await UserModel.updateToken(id, token);</span>
  444. <span class="cstat-no" title="statement not covered" > return res.status(200).json({</span>
  445. status: "success",
  446. code: 200,
  447. data: {
  448. token,
  449. },
  450. });
  451. } catch (e) {
  452. <span class="cstat-no" title="statement not covered" > next(e);</span>
  453. }
  454. };
  455. &nbsp;
  456. const logout = <span class="fstat-no" title="function not covered" >as</span>ync (req, res, next) =&gt; {
  457. const id = <span class="cstat-no" title="statement not covered" >req.user.id;</span>
  458. const user = <span class="cstat-no" title="statement not covered" >await UserModel.findById(id);</span>
  459. <span class="cstat-no" title="statement not covered" > if (!user) {</span>
  460. <span class="cstat-no" title="statement not covered" > return res.status(401).json({</span>
  461. status: "error",
  462. code: 401,
  463. data: "UNAUTHORIZED",
  464. message: "Invalid credentials",
  465. });
  466. }
  467. <span class="cstat-no" title="statement not covered" > await UserModel.updateToken(id, null);</span>
  468. <span class="cstat-no" title="statement not covered" > return res.status(204).json({});</span>
  469. };
  470. const getCurrent = <span class="fstat-no" title="function not covered" >as</span>ync (req, res, next) =&gt; {
  471. const user = <span class="cstat-no" title="statement not covered" >req.user;</span>
  472. <span class="cstat-no" title="statement not covered" > if (!user) {</span>
  473. <span class="cstat-no" title="statement not covered" > return res.status(401).json({</span>
  474. status: "error",
  475. code: 401,
  476. data: "UNAUTHORIZED",
  477. message: "Invalid credentials",
  478. });
  479. }
  480. &nbsp;
  481. <span class="cstat-no" title="statement not covered" > return res.status(200).json({</span>
  482. status: "success",
  483. code: 200,
  484. data: {
  485. user,
  486. },
  487. });
  488. };
  489. const updateSubscription = <span class="fstat-no" title="function not covered" >as</span>ync (req, res, next) =&gt; {
  490. <span class="cstat-no" title="statement not covered" > try {</span>
  491. const id = <span class="cstat-no" title="statement not covered" >req.user.id;</span>
  492. <span class="cstat-no" title="statement not covered" > console.log(req.body);</span>
  493. const update = <span class="cstat-no" title="statement not covered" >await UserModel.updateSubscrip(id, req.body);</span>
  494. <span class="cstat-no" title="statement not covered" > return res.status(200).json({</span>
  495. data: update,
  496. });
  497. } catch (e) {
  498. <span class="cstat-no" title="statement not covered" > next(e);</span>
  499. }
  500. };
  501. &nbsp;
  502. module.exports = {
  503. saveAvatarForStatic,
  504. createNewUser,
  505. logIn,
  506. logout,
  507. getCurrent,
  508. updateSubscription,
  509. };
  510. &nbsp;</pre></td></tr></table></pre>
  511. <div class='push'></div><!-- for sticky footer -->
  512. </div><!-- /wrapper -->
  513. <div class='footer quiet pad2 space-top1 center small'>
  514. Code coverage generated by
  515. <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
  516. at Mon Mar 15 2021 13:23:50 GMT+0200 (GMT+02:00)
  517. </div>
  518. </div>
  519. <script src="../../prettify.js"></script>
  520. <script>
  521. window.onload = function () {
  522. prettyPrint();
  523. };
  524. </script>
  525. <script src="../../sorter.js"></script>
  526. <script src="../../block-navigation.js"></script>
  527. </body>
  528. </html>