Skip to content

Commit

Permalink
LIMS-77: Link Lab Contacts to fedids (#622)
Browse files Browse the repository at this point in the history
* LIMS-77: Link Lab Contacts to fedids

* Use Promise.all rather than $.when

Co-authored-by: Guilherme Francisco <[email protected]>

---------

Co-authored-by: Mark Williams <[email protected]>
Co-authored-by: Guilherme Francisco <[email protected]>
  • Loading branch information
3 people authored Sep 13, 2023
1 parent e533e07 commit 0568e32
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 18 deletions.
4 changes: 2 additions & 2 deletions api/src/Model/Services/UserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function getUsers($getCount, $isStaffMember, $stringMatch, $page, $sortBy = null
}
}

$rows = $this->db->paginate("SELECT $extc p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, '' as postcode, l.country
$rows = $this->db->paginate("SELECT $extc p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, l.postcode, l.country
FROM person p
LEFT OUTER JOIN proposalhasperson prhp ON prhp.personid = p.personid
LEFT OUTER JOIN labcontact lc ON lc.personid = p.personid
Expand Down Expand Up @@ -253,7 +253,7 @@ function getUsersForProposal($where, $getCount, $page, $sortBy, $proposalid, $cu
}
}

$extc = "p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, '' as postcode, l.country";
$extc = "p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, l.postcode, l.country";
$rows = $this->db->paginate("(SELECT $extc
FROM person p
LEFT OUTER JOIN ProposalHasPerson prhp ON prhp.personid = p.personid
Expand Down
67 changes: 55 additions & 12 deletions api/src/Page/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,62 @@ function _add_contact() {
WHERE c.cardname=:1 and c.proposalid=:2", array($this->arg('CARDNAME'), $this->proposalid));
if (sizeof($cont)) $this->_error('The specified card name already exists');

$this->db->pq("INSERT INTO laboratory (laboratoryid,name,address,city,postcode,country)
VALUES (s_laboratory.nextval, :1, :2, :3, :4, :5) RETURNING laboratoryid INTO :id",
array($this->arg('LABNAME'), $this->arg('ADDRESS'), $this->arg('CITY'), $this->arg('POSTCODE'), $this->arg('COUNTRY')));
$lid = $this->db->id();

$email = $this->has_arg('EMAILADDRESS') ? $this->arg('EMAILADDRESS') : '';
$phone = $this->has_arg('PHONENUMBER') ? $this->arg('PHONENUMBER') : '';

$this->db->pq("INSERT INTO person (personid, givenname, familyname, emailaddress, phonenumber, laboratoryid)
VALUES (s_person.nextval, :1, :2, :3, :4, :5) RETURNING personid INTO :id",
array($this->arg('GIVENNAME'), $this->arg('FAMILYNAME'), $email, $phone, $lid));
if ($this->has_arg('PERSONID')) {
$pid = $this->arg('PERSONID');

$check = $this->db->pq("SELECT c.labcontactid
FROM labcontact c
WHERE c.personid=:1 and c.proposalid=:2", array($pid, $this->proposalid));
if (sizeof($check)) $this->_error('The specified login already has a lab contact for this proposal');

$lab = $this->db->pq("SELECT l.laboratoryid FROM laboratory l
INNER JOIN person p ON l.laboratoryid = p.laboratoryid
WHERE p.personid=:1", array($pid));

if (sizeof($lab)) {
# Update laboratory
$lfields = array('LABNAME', 'ADDRESS', 'CITY', 'COUNTRY', 'POSTCODE');
foreach ($lfields as $i => $f) {
if ($this->has_arg($f)) {
$c = $f == 'LABNAME' ? 'NAME' : $f;
$this->db->pq('UPDATE laboratory SET '.$c.'=:1 WHERE laboratoryid=:2', array($this->arg($f), $lab['LABORATORYID']));
}
}
}
else {
# Create laboratory
$this->db->pq("INSERT INTO laboratory (laboratoryid,name,address,city,postcode,country)
VALUES (s_laboratory.nextval, :1, :2, :3, :4, :5) RETURNING laboratoryid INTO :id",
array($this->arg('LABNAME'), $this->arg('ADDRESS'), $this->arg('CITY'), $this->arg('POSTCODE'), $this->arg('COUNTRY')));
$lid = $this->db->id();
$this->db->pq('UPDATE person SET laboratoryid=:1 WHERE personid=:2', array($lid, $pid));
}

# Update person
$pfields = array('FAMILYNAME', 'GIVENNAME', 'PHONENUMBER', 'EMAILADDRESS');
foreach ($pfields as $i => $f) {
if ($this->has_arg($f)) {
$this->db->pq('UPDATE person SET '.$f.'=:1 WHERE personid=:2', array($this->arg($f), $pid));
}
}

}
else {

$pid = $this->db->id();
$this->db->pq("INSERT INTO laboratory (laboratoryid,name,address,city,postcode,country)
VALUES (s_laboratory.nextval, :1, :2, :3, :4, :5) RETURNING laboratoryid INTO :id",
array($this->arg('LABNAME'), $this->arg('ADDRESS'), $this->arg('CITY'), $this->arg('POSTCODE'), $this->arg('COUNTRY')));
$lid = $this->db->id();

$email = $this->has_arg('EMAILADDRESS') ? $this->arg('EMAILADDRESS') : '';
$phone = $this->has_arg('PHONENUMBER') ? $this->arg('PHONENUMBER') : '';

$this->db->pq("INSERT INTO person (personid, givenname, familyname, emailaddress, phonenumber, laboratoryid)
VALUES (s_person.nextval, :1, :2, :3, :4, :5) RETURNING personid INTO :id",
array($this->arg('GIVENNAME'), $this->arg('FAMILYNAME'), $email, $phone, $lid));

$pid = $this->db->id();
}

$c = $this->def_arg('DEFAULTCOURRIERCOMPANY', '');
$ca = $this->has_arg('COURIERACCOUNT') ? $this->arg('COURIERACCOUNT') : '';
Expand Down
3 changes: 3 additions & 0 deletions client/src/js/models/labcontact.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ define(['backbone'], function(Backbone) {
required: true,
pattern: 'wwsdash',
},
PERSONID: {
required: false,
},
FAMILYNAME: {
required: true,
pattern: 'wwdash',
Expand Down
51 changes: 47 additions & 4 deletions client/src/js/modules/contact/views/addcontact.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
define(['views/form',
'models/labcontact',
'collections/countries',
'collections/users',
'templates/contact/contactadd.html',
'jquery',
'backbone',

'backbone-validation',

], function(FormView,
Contact, Countries,
Contact, Countries, Users,
template, $_, Backbone) {


Expand All @@ -17,26 +18,68 @@ define(['views/form',

ui: {
country: 'select[name=COUNTRY]',
pid: 'select[name=PERSONID]',
cn: 'input[name=CARDNAME]',
fn: 'input[name=FAMILYNAME]',
gn: 'input[name=GIVENNAME]',
pn: 'input[name=PHONENUMBER]',
ea: 'input[name=EMAILADDRESS]',
ln: 'input[name=LABNAME]',
ad: 'textarea[name=ADDRESS]',
ci: 'input[name=CITY]',
pc: 'input[name=POSTCODE]',
},

events: {
'change @ui.pid': 'fillUserInfo',
},

fillUserInfo: function() {
var u = this.users.findWhere({ PERSONID: this.ui.pid.val() })
if (u){
this.ui.cn.val(u.get('FULLNAME'))
this.ui.fn.val(u.get('FAMILYNAME'))
this.ui.gn.val(u.get('GIVENNAME'))
this.ui.pn.val(u.get('PHONENUMBER'))
this.ui.ea.val(u.get('EMAILADDRESS'))
this.ui.ln.val(u.get('LABNAME'))
this.ui.ad.val(u.get('ADDRESS'))
this.ui.ci.val(u.get('CITY'))
this.ui.pc.val(u.get('POSTCODE'))
this.ui.country.val(u.get('COUNTRY'))
}
},

initialize: function() {
this.countries = new Countries()
this.countries.state.pageSize = 9999
this.ready = this.countries.fetch()
this.users = new Users()
this.users.queryParams.login = 1
},

createModel: function() {
this.model = new Contact()
},

onRender: function() {
$.when(this.ready).done(this.populateCountries.bind(this))
Promise.all([this.countries.fetch(), this.users.fetch()]).then(() => {
this.populateCountries();
this.populateUsers();
})
},

populateCountries: function() {
this.ui.country.html(this.countries.opts())
},

populateUsers: function() {
// only want to display the current user
var u = this.users.findWhere({ PERSONID: app.personid.toString() })
var opts = '<option value=""> - </option>'
opts += '<option value="'+u.get('PERSONID')+'">'+u.get('FULLNAME')+'</option>'
this.ui.pid.html(opts)
},

success: function(model, response, options) {
console.log('success from contact add')
if (this.getOption('dialog')) {
Expand Down
7 changes: 7 additions & 0 deletions client/src/js/templates/contact/contactadd.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ <h1>Add New Home Lab Contact</h1>

<li class="head">Contact Details</li>

<li>
<label>Login
<span class="small">Choose your login to prefill information, or leave blank to add someone else</span>
</label>
<select name="PERSONID"></select>
</li>

<li>
<label>Family Name</label>
<input type="text" name="FAMILYNAME" />
Expand Down

0 comments on commit 0568e32

Please sign in to comment.