Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 'use strict'; const logger = require('../../services/logger'); module.exports = function(Site) { Site.createSite = async newSite => { try { const {organizationId, streetAddress, city, postalCode, country} = newSite; if (!city || city.length === 0) throw new Error('City cant be empty'); if (!streetAddress || streetAddress.length === 0) throw new Error('Street address cant be empty'); if (!postalCode || postalCode.length === 0) throw new Error('Postal code cant be empty'); if (!country || country.length === 0) throw new Error('Country cant be empty'); const address = await Site.app.models.Address.create({ StreetAddress: streetAddress, City: city, PostalCode: postalCode, Country: country, CreatedAt: new Date() }); const site = await Site.app.models.Site.create({ OrganizationId: organizationId, AddressId: address.id, CreatedAt: new Date() }); const fullSite = await Site.findById(site.id, { include: ['address'] }); return fullSite; } catch (error) { logger.error(`Error creating Site, ${error}`); throw error; } }; Site.remoteMethod('createSite', { http: {verb: 'post'}, accepts: [ { arg: 'newSite', type: 'object' } ], returns: { arg: 'response', type: 'object' } }); Site.updateSite = async updatedSite => { try { const site = await Site.findById(updatedSite.id); const {streetAddress, city, postalCode, country} = updatedSite; if (!city || city.length === 0) throw new Error('City cant be empty'); if (!streetAddress || streetAddress.length === 0) throw new Error('Street address cant be empty'); if (!postalCode || postalCode.length === 0) throw new Error('Postal code cant be empty'); if (!country || country.length === 0) throw new Error('Country cant be empty'); await site.updateAttributes({ StreetAddress: streetAddress, City: city, PostalCode: postalCode, Country: country, UpdatedAt: new Date() }); const address = await Site.app.models.Address.findById(site.AddressId); await address.updateAttributes({ StreetAddress: updatedSite.streetAddress, City: updatedSite.city, PostalCode: updatedSite.postalCode, Country: updatedSite.country, UpdatedAt: new Date() }); const fullSite = await Site.findById(site.id, { include: ['address'] }); return fullSite; } catch (error) { logger.error(`Error updating Site, ${error}`); throw error; } }; Site.remoteMethod('updateSite', { http: {verb: 'post'}, accepts: [ { arg: 'updatedSite', type: 'object' } ], returns: { arg: 'response', type: 'object' } }); Site.deleteSite = async siteId => { try { const resources = await Site.app.models.Resource.find({ where: { SiteId: siteId } }); if (resources.length > 0) { let error = new Error("Can't delete Site with employees!"); error.statusCode = 400; error.name = 'error_employees'; throw error; } const site = await Site.findById(siteId); site.updateAttributes({ DeletedAt: new Date() }); return site; } catch (error) { logger.error(`Error deleting Site, ${error}`); throw error; } }; Site.remoteMethod('deleteSite', { http: {verb: 'post'}, accepts: [ { arg: 'siteId', type: 'number' } ], returns: { arg: 'response', type: 'object' } }); Site.findSites = async organizationId => { try { const sites = await Site.find({ include: 'address', where: { OrganizationId: organizationId } }); return sites; } catch (e) { logger.error(`Error finding site, ${e}`); throw new Error("Error finding sites!"); } }; Site.remoteMethod('findSites', { http: {verb: 'get'}, accepts: [ { arg: 'organizationId', type: 'number' } ], returns: { arg: 'response', type: 'object', root: true } }); Site.findAllSites = async() => { try { const sites = await Site.find({ include: ['address', {organization: 'organizationTranslations'}] }); return sites; } catch (e) { logger.error(`Error finding site, ${e}`); throw new Error("Error finding sites!"); } }; Site.remoteMethod('findAllSites', { http: {verb: 'get'}, returns: { arg: 'response', type: 'object', root: true } }); }; |