This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Institution.txt
31 lines (30 loc) · 3.94 KB
/
Institution.txt
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
Type: Institution
Version: 2.0
Description: Create or (un)withdraw an institution.
Template: {"receiver":{"type":"addr","desc":"Address van de onderwijsinstelling.","name":"Onderwijsinstelling"},"iri":{"type":"str","desc":"IRI van waar het profiel gevonden kan worden.","name":"IRI"},"name":{"type":"str","desc":"Naam van onderwijsinstelling.","name":"Naam"},"allow":{"type":"bool","desc":"Toestaan of niet meer toestaan.","name":"Toegestaan"}}
Init: YXdhaXQgcXVlcnkoIkNSRUFURSIsICJpbnN0aXR1dGlvbnMiLCAiKGluc3RpdHV0aW9uIFZBUkNIQVIoMzUpIFBSSU1BUlkgS0VZLCBpcmkgVkFSQ0hBUigyNTYpIE5PVCBOVUxMLCByZXZva2VkIEJJR0lOVCk7IiwgW10pOw0KYXdhaXQgcXVlcnkoIkNSRUFURSIsICJpbnN0aXR1dGlvbl9uYW1lcyIsICIoaW5zdGl0dXRpb24gVkFSQ0hBUigzNSkgTk9UIE5VTEwgUkVGRVJFTkNFUyBpbnN0aXR1dGlvbnMoaW5zdGl0dXRpb24pLCAiICsNCiAgICAibmFtZSBWQVJDSEFSKDEyOCkgTk9UIE5VTEwsIHN0YXJ0X3RpbWUgQklHSU5UIE5PVCBOVUxMLCBlbmRfdGltZSBCSUdJTlQpOyIsIFtdKTs=
Code: Ly9Pbmx5IHN1cmYgaXMgYWxsb3dlZCB0byBjcmVhdGUgYW5kIHdpdGhkcmF3IGluc3RpdHV0aW9ucw0KaWYgKGZyb20gIT09IHByb2Nlc3Nvcikgew0KICAgIHJldHVybiAiT25seSBTVVJGIGlzIGFsbG93ZWQgdG8gY3JlYXRlLyh1bil3aXRoZHJhdyBpbnN0aXR1dGlvbnMuIjsNCn0NCmlmIChwYXlsb2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Hx8IG9sZE5hbWUubmFtZSAhPT0gcGF5bG9hZC5uYW1lKSB7DQogICAgYXdhaXQgcXVlcnkoIklOU0VSVCIsICJpbnN0aXR1dGlvbl9uYW1lcyIsICIoaW5zdGl0dXRpb24sIG5hbWUsIHN0YXJ0X3RpbWUpIFZBTFVFUyAoJDEsICQyLCAkMyk7IiwgW3BheWxvYWQucmVjZWl2ZXIsIHBheWxvYWQubmFtZSwgcHJldmlvdXNCbG9ja1RpbWVzdGFtcF0pOw0KfQ0KcmV0dXJuICJPSyI7
Init as text:
await query("CREATE", "institutions", "(institution VARCHAR(35) PRIMARY KEY, iri VARCHAR(256) NOT NULL, revoked BIGINT);", []);
await query("CREATE", "institution_names", "(institution VARCHAR(35) NOT NULL REFERENCES institutions(institution), " +
"name VARCHAR(128) NOT NULL, start_time BIGINT NOT NULL, end_time BIGINT);", []);
Code as text:
//Only surf is allowed to create and withdraw institutions
if (from !== processor) {
return "Only SURF is allowed to create/(un)withdraw institutions.";
}
if (payload.name.length === 0 || payload.name.length > 128) {
return "Invalid name.";
}
//Add (or update) the institution
await query("INSERT", "institutions", "(institution, iri, revoked) VALUES ($1, $2, $3) ON CONFLICT "
+ "ON CONSTRAINT institutions_pkey DO UPDATE SET revoked = $3;", [payload.receiver, payload.iri, payload.allow ? undefined : previousBlockTimestamp]);
//Add (or update) the name if needed
const oldName = (await query("SELECT", "institution_names", "WHERE institution = $1 AND end_time IS NULL;", [payload.receiver])).rows[0];
if (oldName !== undefined && oldName.name !== payload.name) {
await query("UPDATE", "institution_names", "SET end_time = $2 WHERE institution = $1 AND end_time IS NULL;", [payload.receiver, previousBlockTimestamp]);
}
if (oldName === undefined || oldName.name !== payload.name) {
await query("INSERT", "institution_names", "(institution, name, start_time) VALUES ($1, $2, $3);", [payload.receiver, payload.name, previousBlockTimestamp]);
}
return "OK";