This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
forked from cozy/cozy-clearance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.coffee
110 lines (87 loc) · 3.33 KB
/
controller.coffee
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
clearance = require './index'
async = require 'async'
cozydb = require 'cozy-db-pouchdb'
class DataPoint extends cozydb.Model
@schema:
name: String
value: String
type: String
Contact = cozydb.getModel 'Contact',
fn : String
n : String
_attachments : Object
datapoints : [DataPoint]
module.exports = (options) ->
out = {}
mailSubject = options.mailSubject
mailTemplate = options.mailTemplate
# send a share mail
sendMail = (doc, key, cb) ->
rule = doc.clearance.filter((rule) -> rule.key is key)[0]
doc.getPublicURL (err, url) =>
return cb err if err
url += '?key=' + rule.key
emailOptions = {doc, url, rule}
async.parallel [
(cb) -> mailSubject emailOptions, cb
(cb) -> mailTemplate emailOptions, cb
], (err, results) ->
[subject, htmlContent] = results
emailInfo =
to: rule.email
subject: subject
content: url
html: htmlContent
cozydb.api.sendMailFromUser emailInfo, cb
# change the whole clearance object
out.change = (req, res, next) ->
{clearance} = req.body
req.doc.updateAttributes clearance: clearance, (err) ->
return next err if err
res.send req.doc
# send multiple mails
# expect body = [<rule>]
out.sendAll = (req, res, next) ->
toSend = req.body
sent = []
async.each toSend, (rule, cb) ->
sent.push rule.key
sendMail req.doc, rule.key, cb
, (err) ->
return next err if err
newClearance = req.doc.clearance.map (rule) ->
rule.sent = true if rule.key in sent
return rule
req.doc.updateAttributes clearance: newClearance, (err) ->
return next err if err
res.send req.doc
out.getEmailsFromContactFields = (contact) ->
emails = contact.datapoints?.filter (dp) -> dp.name is 'email'
emails = emails.map (dp) -> dp.value
emails
# take directly full name or build it from the name field.
out.getContactFullName = (contact) ->
contact.fn or contact.n?.split(';')[0..1].join(' ')
# contact list for autocomplete
out.contactList = (req, res, next) ->
Contact.request 'all', (err, contacts) ->
return next err if err
res.send contacts.map (contact) ->
name = out.getContactFullName contact
emails = out.getEmailsFromContactFields contact
return simple =
id: contact.id
hasPicture: contact._attachments?.picture?
name: name or '?'
emails: emails or []
out.contactPicture = (req, res, next) ->
Contact.find req.params.contactid, (err, contact) ->
return next err if err
unless contact._attachments?.picture
err = new Error('not found')
err.status = 404
return next err
stream = contact.getFile 'picture', (err) ->
return res.error 500, "File fetching failed.", err if err
stream.pipe res
return out