Archived
2

Merge pull request #4 in ~VLE2FE/dfop-api from condition to develop

* commit 'c9be3f4eb7c7f9d1fc5d3cc84915d0e37eeab18f':
  PUT method for condition
  DELETE and GET methods for condition
  POST /condition/new
This commit is contained in:
Veit Lukas (PEA4-Fe) 2020-05-11 13:06:32 +02:00
commit a82361c818
19 changed files with 1504 additions and 40 deletions

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="dataSourceStorageLocal">
<data-source name="@localhost" uuid="46f112fc-d60d-4217-873f-f5ffea06180c">
<database-info product="Mongo DB" version="4.2.5" jdbc-version="4.2" driver-name="MongoDB JDBC Driver" driver-version="1.7.1" dbms="MONGO" exact-version="4.2.5" exact-driver-version="1.7" />
<case-sensitivity plain-identifiers="mixed" quoted-identifiers="mixed" />
<secret-storage>master_key</secret-storage>
<schema-mapping>
<introspection-scope>
<node kind="schema" negative="1" />
</introspection-scope>
</schema-mapping>
</data-source>
</component>
</project>

View File

@ -0,0 +1,584 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataSource name="@localhost">
<database-model serializer="dbm" dbms="MONGO" family-id="MONGO" format-version="4.18">
<root id="1">
<ServerVersion>4.2.5</ServerVersion>
</root>
<schema id="2" parent="1" name="admin"/>
<schema id="3" parent="1" name="config"/>
<schema id="4" parent="1" name="dfopdb"/>
<schema id="5" parent="1" name="dfopdb_test"/>
<schema id="6" parent="1" name="local"/>
<schema id="7" parent="1" name="test">
<Current>1</Current>
</schema>
<table id="8" parent="3" name="system.sessions"/>
<table id="9" parent="4" name="materials"/>
<table id="10" parent="4" name="measurement_templates"/>
<table id="11" parent="4" name="note_fields"/>
<table id="12" parent="4" name="notes"/>
<table id="13" parent="4" name="samples"/>
<table id="14" parent="4" name="treatment_templates"/>
<table id="15" parent="4" name="users"/>
<table id="16" parent="5" name="materials"/>
<table id="17" parent="5" name="measurement_templates"/>
<table id="18" parent="5" name="note_fields"/>
<table id="19" parent="5" name="notes"/>
<table id="20" parent="5" name="samples"/>
<table id="21" parent="5" name="treatment_templates"/>
<table id="22" parent="5" name="users"/>
<table id="23" parent="6" name="startup_log"/>
<table id="24" parent="7" name="a"/>
<table id="25" parent="7" name="b"/>
<column id="26" parent="9" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="27" parent="9" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="28" parent="9" name="carbon_fiber">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="29" parent="9" name="glass_fiber">
<Position>3</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="30" parent="9" name="group">
<Position>4</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="31" parent="9" name="mineral">
<Position>5</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="32" parent="9" name="name">
<Position>6</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="33" parent="9" name="numbers">
<Position>7</Position>
<DataType>list(0)|4999545s</DataType>
</column>
<column id="34" parent="9" name="numbers._id">
<Position>8</Position>
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="35" parent="9" name="numbers.color">
<Position>9</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="36" parent="9" name="numbers.number">
<Position>10</Position>
<DataType>Double(0)|8s</DataType>
</column>
<column id="37" parent="9" name="supplier">
<Position>11</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="38" parent="12" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="39" parent="12" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="40" parent="12" name="comment">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="41" parent="12" name="sample_references">
<Position>3</Position>
<DataType>list(0)|4999545s</DataType>
</column>
<column id="42" parent="13" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="43" parent="13" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="44" parent="13" name="batch">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="45" parent="13" name="color">
<Position>3</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="46" parent="13" name="material_id">
<Position>4</Position>
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="47" parent="13" name="note_id">
<Position>5</Position>
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="48" parent="13" name="number">
<Position>6</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="49" parent="13" name="type">
<Position>7</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="50" parent="13" name="user_id">
<Position>8</Position>
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="51" parent="15" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="52" parent="15" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="53" parent="15" name="device_name">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="54" parent="15" name="email">
<Position>3</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="55" parent="15" name="key">
<Position>4</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="56" parent="15" name="level">
<Position>5</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="57" parent="15" name="location">
<Position>6</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="58" parent="15" name="name">
<Position>7</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="59" parent="15" name="pass">
<Position>8</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="60" parent="16" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="61" parent="16" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="62" parent="16" name="carbon_fiber">
<Position>2</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="63" parent="16" name="glass_fiber">
<Position>3</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="64" parent="16" name="group">
<Position>4</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="65" parent="16" name="mineral">
<Position>5</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="66" parent="16" name="name">
<Position>6</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="67" parent="16" name="numbers">
<Position>7</Position>
<DataType>list(0)|4999545s</DataType>
</column>
<column id="68" parent="16" name="numbers.color">
<Position>8</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="69" parent="16" name="numbers.number">
<Position>9</Position>
<DataType>Double(0)|8s</DataType>
</column>
<column id="70" parent="16" name="supplier">
<Position>10</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="71" parent="17" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="72" parent="17" name="name">
<Position>1</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="73" parent="17" name="parameters">
<Position>2</Position>
<DataType>list(0)|4999545s</DataType>
</column>
<column id="74" parent="17" name="parameters.name">
<Position>3</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="75" parent="17" name="parameters.range">
<Position>4</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="76" parent="17" name="parameters.range.max">
<Position>5</Position>
<DataType>Double(0)|8s</DataType>
</column>
<column id="77" parent="17" name="parameters.range.min">
<Position>6</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="78" parent="18" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="79" parent="18" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="80" parent="18" name="name">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="81" parent="18" name="qty">
<Position>3</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="82" parent="19" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="83" parent="19" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="84" parent="19" name="comment">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="85" parent="19" name="custom_fields">
<Position>3</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="86" parent="19" name="custom_fields.another_field">
<Position>4</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="87" parent="19" name="custom_fields.not allowed for new applications">
<Position>5</Position>
<DataType>Boolean|12s</DataType>
</column>
<column id="88" parent="19" name="sample_references">
<Position>6</Position>
<DataType>list(0)|4999545s</DataType>
</column>
<column id="89" parent="19" name="sample_references.id">
<Position>7</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="90" parent="19" name="sample_references.relation">
<Position>8</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="91" parent="20" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="92" parent="20" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="93" parent="20" name="batch">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="94" parent="20" name="color">
<Position>3</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="95" parent="20" name="material_id">
<Position>4</Position>
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="96" parent="20" name="note_id">
<Position>5</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="97" parent="20" name="number">
<Position>6</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="98" parent="20" name="type">
<Position>7</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="99" parent="20" name="user_id">
<Position>8</Position>
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="100" parent="20" name="validated">
<Position>9</Position>
<DataType>Boolean|12s</DataType>
</column>
<column id="101" parent="21" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="102" parent="21" name="name">
<Position>1</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="103" parent="21" name="parameters">
<Position>2</Position>
<DataType>list(0)|4999545s</DataType>
</column>
<column id="104" parent="21" name="parameters.name">
<Position>3</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="105" parent="21" name="parameters.range">
<Position>4</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="106" parent="21" name="parameters.range.max">
<Position>5</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="107" parent="21" name="parameters.range.min">
<Position>6</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="108" parent="21" name="parameters.range.values">
<Position>7</Position>
<DataType>array(0)|2003s</DataType>
</column>
<column id="109" parent="22" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="110" parent="22" name="__v">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="111" parent="22" name="device_name">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="112" parent="22" name="email">
<Position>3</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="113" parent="22" name="key">
<Position>4</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="114" parent="22" name="level">
<Position>5</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="115" parent="22" name="location">
<Position>6</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="116" parent="22" name="name">
<Position>7</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="117" parent="22" name="pass">
<Position>8</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="118" parent="23" name="_id">
<DataType>String(0)|12s</DataType>
</column>
<column id="119" parent="23" name="buildinfo">
<Position>1</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="120" parent="23" name="buildinfo.allocator">
<Position>2</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="121" parent="23" name="buildinfo.bits">
<Position>3</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="122" parent="23" name="buildinfo.buildEnvironment">
<Position>4</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="123" parent="23" name="buildinfo.buildEnvironment.cc">
<Position>5</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="124" parent="23" name="buildinfo.buildEnvironment.ccflags">
<Position>6</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="125" parent="23" name="buildinfo.buildEnvironment.cxx">
<Position>7</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="126" parent="23" name="buildinfo.buildEnvironment.cxxflags">
<Position>8</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="127" parent="23" name="buildinfo.buildEnvironment.distarch">
<Position>9</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="128" parent="23" name="buildinfo.buildEnvironment.distmod">
<Position>10</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="129" parent="23" name="buildinfo.buildEnvironment.linkflags">
<Position>11</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="130" parent="23" name="buildinfo.buildEnvironment.target_arch">
<Position>12</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="131" parent="23" name="buildinfo.buildEnvironment.target_os">
<Position>13</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="132" parent="23" name="buildinfo.debug">
<Position>14</Position>
<DataType>Boolean|12s</DataType>
</column>
<column id="133" parent="23" name="buildinfo.gitVersion">
<Position>15</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="134" parent="23" name="buildinfo.javascriptEngine">
<Position>16</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="135" parent="23" name="buildinfo.maxBsonObjectSize">
<Position>17</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="136" parent="23" name="buildinfo.modules">
<Position>18</Position>
<DataType>list(0)|4999545s</DataType>
</column>
<column id="137" parent="23" name="buildinfo.openssl">
<Position>19</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="138" parent="23" name="buildinfo.openssl.running">
<Position>20</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="139" parent="23" name="buildinfo.storageEngines">
<Position>21</Position>
<DataType>array(0)|2003s</DataType>
</column>
<column id="140" parent="23" name="buildinfo.sysInfo">
<Position>22</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="141" parent="23" name="buildinfo.targetMinOS">
<Position>23</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="142" parent="23" name="buildinfo.version">
<Position>24</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="143" parent="23" name="buildinfo.versionArray">
<Position>25</Position>
<DataType>array(0)|2003s</DataType>
</column>
<column id="144" parent="23" name="cmdLine">
<Position>26</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="145" parent="23" name="cmdLine.config">
<Position>27</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="146" parent="23" name="cmdLine.net">
<Position>28</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="147" parent="23" name="cmdLine.net.bindIp">
<Position>29</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="148" parent="23" name="cmdLine.net.port">
<Position>30</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="149" parent="23" name="cmdLine.service">
<Position>31</Position>
<DataType>Boolean|12s</DataType>
</column>
<column id="150" parent="23" name="cmdLine.storage">
<Position>32</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="151" parent="23" name="cmdLine.storage.dbPath">
<Position>33</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="152" parent="23" name="cmdLine.storage.journal">
<Position>34</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="153" parent="23" name="cmdLine.storage.journal.enabled">
<Position>35</Position>
<DataType>Boolean|12s</DataType>
</column>
<column id="154" parent="23" name="cmdLine.systemLog">
<Position>36</Position>
<DataType>map(0)|4999544s</DataType>
</column>
<column id="155" parent="23" name="cmdLine.systemLog.destination">
<Position>37</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="156" parent="23" name="cmdLine.systemLog.logAppend">
<Position>38</Position>
<DataType>Boolean|12s</DataType>
</column>
<column id="157" parent="23" name="cmdLine.systemLog.path">
<Position>39</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="158" parent="23" name="hostname">
<Position>40</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="159" parent="23" name="pid">
<Position>41</Position>
<DataType>Long(0)|12s</DataType>
</column>
<column id="160" parent="23" name="startTime">
<Position>42</Position>
<DataType>Date(0)|91s</DataType>
</column>
<column id="161" parent="23" name="startTimeLocal">
<Position>43</Position>
<DataType>String(0)|12s</DataType>
</column>
<column id="162" parent="24" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="163" parent="24" name="x">
<Position>1</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="164" parent="24" name="y">
<Position>2</Position>
<DataType>Integer|4s</DataType>
</column>
<column id="165" parent="25" name="_id">
<DataType>ObjectId(0)|12s</DataType>
</column>
<column id="166" parent="25" name="s">
<Position>1</Position>
<DataType>String(0)|12s</DataType>
</column>
</database-model>
</dataSource>

View File

@ -27,7 +27,7 @@ info:
<li>no whitespace</li>
<li>at least 8 characters</li>
</ul>
# TODO: Link to new documentation page
servers:

View File

@ -2,7 +2,7 @@
parameters:
- $ref: 'api.yaml#/components/parameters/Id'
get:
summary: TODO condition by id
summary: condition by id
description: 'Auth: all, levels: read, write, maintain, dev, admin'
tags:
- /condition
@ -22,8 +22,67 @@
500:
$ref: 'api.yaml#/components/responses/500'
put:
summary: TODO add/change condition
summary: change condition
description: 'Auth: basic, levels: write, maintain, dev, admin <br>Only maintain and admin are allowed to reference samples created by another user'
tags:
- /condition
security:
- BasicAuth: []
requestBody:
required: true
content:
application/json:
schema:
allOf:
- $ref: 'api.yaml#/components/schemas/_Id'
properties:
number:
type: string
example: B1
parameters:
type: object
responses:
200:
description: condition details
content:
application/json:
schema:
$ref: 'api.yaml#/components/schemas/Condition'
400:
$ref: 'api.yaml#/components/responses/400'
401:
$ref: 'api.yaml#/components/responses/401'
403:
$ref: 'api.yaml#/components/responses/403'
404:
$ref: 'api.yaml#/components/responses/404'
500:
$ref: 'api.yaml#/components/responses/500'
delete:
summary: delete condition
description: 'Auth: basic, levels: write, maintain, dev, admin'
tags:
- /condition
security:
- BasicAuth: []
responses:
200:
$ref: 'api.yaml#/components/responses/Ok'
400:
$ref: 'api.yaml#/components/responses/400'
401:
$ref: 'api.yaml#/components/responses/401'
403:
$ref: 'api.yaml#/components/responses/403'
404:
$ref: 'api.yaml#/components/responses/404'
500:
$ref: 'api.yaml#/components/responses/500'
/condition/new:
post:
summary: add condition
description: 'Auth: basic, levels: write, maintain, dev, admin <br>Only maintain and admin are allowed to reference samples created by another user'
tags:
- /condition
security:
@ -47,27 +106,5 @@
$ref: 'api.yaml#/components/responses/401'
403:
$ref: 'api.yaml#/components/responses/403'
404:
$ref: 'api.yaml#/components/responses/404'
500:
$ref: 'api.yaml#/components/responses/500'
delete:
summary: TODO delete condition
description: 'Auth: basic, levels: write, maintain, dev, admin'
tags:
- /condition
security:
- BasicAuth: []
responses:
200:
$ref: 'api.yaml#/components/responses/Ok'
400:
$ref: 'api.yaml#/components/responses/400'
401:
$ref: 'api.yaml#/components/responses/401'
403:
$ref: 'api.yaml#/components/responses/403'
404:
$ref: 'api.yaml#/components/responses/404'
500:
$ref: 'api.yaml#/components/responses/500'

View File

@ -22,7 +22,7 @@
500:
$ref: 'api.yaml#/components/responses/500'
put:
summary: TODO add/change measurement
summary: TODO change measurement
description: 'Auth: basic, levels: write, maintain, dev, admin'
tags:
- /measurement
@ -69,5 +69,35 @@
$ref: 'api.yaml#/components/responses/403'
404:
$ref: 'api.yaml#/components/responses/404'
500:
$ref: 'api.yaml#/components/responses/500'
/measurement/new:
post:
summary: TODO add measurement
description: 'Auth: basic, levels: write, maintain, dev, admin'
tags:
- /measurement
security:
- BasicAuth: []
requestBody:
required: true
content:
application/json:
schema:
$ref: 'api.yaml#/components/schemas/Measurement'
responses:
200:
description: measurement details
content:
application/json:
schema:
$ref: 'api.yaml#/components/schemas/Measurement'
400:
$ref: 'api.yaml#/components/responses/400'
401:
$ref: 'api.yaml#/components/responses/401'
403:
$ref: 'api.yaml#/components/responses/403'
500:
$ref: 'api.yaml#/components/responses/500'

View File

@ -42,7 +42,7 @@
$ref: 'api.yaml#/components/responses/500'
put:
summary: change sample
description: 'Auth: basic, levels: write, maintain, dev, admin, only maintain and admin are allowed to edit samples created by another user'
description: 'Auth: basic, levels: write, maintain, dev, admin <br>Only maintain and admin are allowed to edit samples created by another user'
tags:
- /sample
security:
@ -72,7 +72,7 @@
$ref: 'api.yaml#/components/responses/500'
delete:
summary: delete sample
description: 'Auth: basic, levels: write, maintain, dev, admin, only maintain and admin are allowed to edit samples created by another user'
description: 'Auth: basic, levels: write, maintain, dev, admin <br>Only maintain and admin are allowed to edit samples created by another user'
tags:
- /sample
security:

View File

@ -120,6 +120,9 @@ Condition:
properties:
sample_id:
$ref: 'api.yaml#/components/schemas/Id'
number:
type: string
example: B1
parameters:
type: object
treatment_template:

View File

@ -49,6 +49,7 @@ app.use('/', require('./routes/sample'));
app.use('/', require('./routes/material'));
app.use('/', require('./routes/template'));
app.use('/', require('./routes/user'));
app.use('/', require('./routes/condition'));
// static files
app.use('/static', express.static('static'));

12
src/models/condition.ts Normal file
View File

@ -0,0 +1,12 @@
import mongoose from 'mongoose';
import SampleModel from './sample';
import TreatmentTemplateModel from './treatment_template';
const ConditionSchema = new mongoose.Schema({
sample_id: {type: mongoose.Schema.Types.ObjectId, ref: SampleModel},
number: String,
parameters: mongoose.Schema.Types.Mixed,
treatment_template: {type: mongoose.Schema.Types.ObjectId, ref: TreatmentTemplateModel}
});
export default mongoose.model('condition', ConditionSchema);

View File

@ -0,0 +1,516 @@
import should from 'should/as-function';
import ConditionModel from '../models/condition';
import TestHelper from "../test/helper";
describe('/condition', () => {
let server;
before(done => TestHelper.before(done));
beforeEach(done => server = TestHelper.beforeEach(server, done));
afterEach(done => TestHelper.afterEach(server, done));
describe('GET /condition/{id}', () => {
it('returns the right condition', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', parameters: {material: 'copper', weeks: 3}, treatment_template: '200000000000000000000001'}
});
});
it('returns the right condition for an API key', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/condition/700000000000000000000001',
auth: {key: 'janedoe'},
httpStatus: 200,
res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', parameters: {material: 'copper', weeks: 3}, treatment_template: '200000000000000000000001'}
});
});
it('rejects an invalid id', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/condition/70000000000t000000000001',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects an unknown id', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/condition/000000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/condition/700000000000000000000001',
httpStatus: 401
});
});
});
describe('PUT /condition{id}', () => {
it('returns the right condition', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {},
res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}}
});
});
it('keeps unchanged properties', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {parameters: {material: 'copper', weeks: 3}},
res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}}
});
});
it('changes the given properties', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {parameters: {material: 'hot air', weeks: 10}}
}).end((err, res) => {
if (err) return done(err);
should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}});
ConditionModel.findById('700000000000000000000001').lean().exec((err, data: any) => {
if (err) return done(err);
should(data.sample_id.toString()).be.eql('400000000000000000000001');
should(data).have.property('number', 'B1');
should(data.treatment_template.toString()).be.eql('200000000000000000000001');
should(data).have.property('parameters');
should(data.parameters).have.property('material', 'hot air');
should(data.parameters).have.property('weeks', 10);
done();
});
});
});
it('allows changing only one parameter', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {parameters: {weeks: 8}},
res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 8}}
});
});
it('rejects not specified parameters', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {parameters: {xx: 13}},
res: {status: 'Invalid body format', details: '"xx" is not allowed'}
});
});
it('rejects a parameter not in the value range', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {parameters: {material: 'xxx'}},
res: {status: 'Invalid body format', details: '"material" must be one of [copper, hot air]'}
});
});
it('rejects a parameter below minimum range', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {parameters: {weeks: -10}},
res: {status: 'Invalid body format', details: '"weeks" must be larger than or equal to 1'}
});
});
it('rejects a parameter above maximum range', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {parameters: {weeks: 11}},
res: {status: 'Invalid body format', details: '"weeks" must be less than or equal to 10'}
});
});
it('rejects a new treatment_template', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {treatment_template: '200000000000000000000002'},
res: {status: 'Invalid body format', details: '"treatment_template" is not allowed'}
});
});
it('rejects editing a condition for a write user who did not create this condition', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000003',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {parameters: {weeks: 8}}
});
});
it('accepts editing a condition of another user for a maintain/admin user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {parameters: {material: 'hot air', weeks: 10}},
res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}}
});
});
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {key: 'janedoe'},
httpStatus: 401,
req: {parameters: {material: 'hot air', weeks: 10}}
});
});
it('rejects requests form a read user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'user'},
httpStatus: 403,
req: {parameters: {material: 'hot air', weeks: 10}}
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
httpStatus: 401,
req: {parameters: {material: 'hot air', weeks: 10}}
});
});
}); // TODO: how to deal with template changes? Template versioning?
// TODO: rewrite delete methods -> set status for every database collection
describe('DELETE /condition/{id}', () => {
it('deletes the condition', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/700000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 200
}).end((err, res) => {
if (err) return done(err);
should(res.body).be.eql({status: 'OK'});
ConditionModel.findById('700000000000000000000002').lean().exec((err, data) => {
if (err) return done(err);
should(data).be.null();
done();
});
});
});
it('rejects a deleting a condition referenced by measurements');
it('rejects an invalid id', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/70000000000w000000000002',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/700000000000000000000002',
auth: {key: 'janedoe'},
httpStatus: 401
});
});
it('rejects requests from a read user', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/700000000000000000000002',
auth: {basic: 'user'},
httpStatus: 403
});
});
it('rejects a write user deleting a condition belonging to a sample of another user', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/700000000000000000000003',
auth: {basic: 'janedoe'},
httpStatus: 403
});
});
it('accepts an maintain/admin user deleting a condition belonging to a sample of another user', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/700000000000000000000002',
auth: {basic: 'admin'},
httpStatus: 200
}).end((err, res) => {
if (err) return done(err);
should(res.body).be.eql({status: 'OK'});
done();
});
});
it('returns 404 for an unknown id', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/00000000000w000000000002',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/condition/700000000000000000000002',
httpStatus: 401
});
});
});
describe('POST /condition/new', () => { // TODO: sample number generation
it('returns the right condition', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}
}).end((err, res) => {
if (err) return done(err);
should(res.body).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template');
should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('sample_id', '400000000000000000000002');
should(res.body).have.property('number', 'B2');
should(res.body).have.property('treatment_template', '200000000000000000000001');
should(res.body).have.property('parameters');
should(res.body.parameters).have.property('material', 'hot air');
should(res.body.parameters).have.property('weeks', 10);
done();
});
});
it('stores the condition', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}
}).end((err, res) => {
if (err) return done(err);
ConditionModel.findById(res.body._id).lean().exec((err, data: any) => {
if (err) return done(err);
should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', '__v');
should(data).have.property('_id');
should(data.sample_id.toString()).be.eql('400000000000000000000002');
should(data).have.property('number', 'B2');
should(data.treatment_template.toString()).be.eql('200000000000000000000001');
should(data).have.property('parameters');
should(data.parameters).have.property('material', 'hot air');
should(data.parameters).have.property('weeks', 10);
done();
});
});
});
it('rejects an invalid sample id', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '4000000000h0000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"sample_id" with value "4000000000h0000000000002" fails to match the required pattern: /[0-9a-f]{24}/'}
});
});
it('rejects a sample id not available', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '000000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'},
res: {status: 'Sample id not available'}
});
});
it('rejects an invalid treatment_template id', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000h00000000001'},
res: {status: 'Invalid body format', details: '"treatment_template" with value "200000000000h00000000001" fails to match the required pattern: /[0-9a-f]{24}/'}
});
});
it('rejects a treatment_template which does not exist', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '000000000000000000000001'},
res: {status: 'Treatment template not available'}
});
});
it('rejects a condition number already in use for this sample', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000001', number: 'B1', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'},
res: {status: 'Condition number already taken'}
});
});
it('rejects not specified parameters', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10, xx: 12}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"xx" is not allowed'}
});
});
it('rejects missing parameters', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air'}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"weeks" is required'}
});
});
it('rejects a parameter not in the value range', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'xxx', weeks: 10}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"material" must be one of [copper, hot air]'}
});
});
it('rejects a parameter below minimum range', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: -10}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"weeks" must be larger than or equal to 1'}
});
});
it('rejects a parameter above maximum range', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 11}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"weeks" must be less than or equal to 10'}
});
});
it('rejects a missing sample id', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"sample_id" is required'}
});
});
it('rejects a missing treatment_template', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}},
res: {status: 'Invalid body format', details: '"treatment_template" is required'}
});
});
it('rejects a missing number', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'},
res: {status: 'Invalid body format', details: '"number" is required'}
});
});
it('rejects adding a condition to the sample of an other user for a write user', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {sample_id: '400000000000000000000003', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}
});
});
it('accepts adding a condition to the sample of an other user for a maintain/admin user', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'admin'},
httpStatus: 200,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}
}).end((err, res) => {
if (err) return done(err);
should(res.body).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template');
should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('sample_id', '400000000000000000000002');
should(res.body).have.property('number', 'B2');
should(res.body).have.property('treatment_template', '200000000000000000000001');
should(res.body).have.property('parameters');
should(res.body.parameters).have.property('material', 'hot air');
should(res.body.parameters).have.property('weeks', 10);
done();
});
});
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {key: 'janedoe'},
httpStatus: 401,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}
});
});
it('rejects requests from a read user', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
auth: {basic: 'user'},
httpStatus: 403,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/condition/new',
httpStatus: 401,
req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}
});
});
});
});

126
src/routes/condition.ts Normal file
View File

@ -0,0 +1,126 @@
import express from 'express';
import mongoose from 'mongoose';
import ConditionValidate from './validate/condition';
import ParametersValidate from './validate/parameters';
import res400 from './validate/res400';
import SampleModel from '../models/sample';
import ConditionModel from '../models/condition';
import TreatmentTemplateModel from '../models/treatment_template';
import IdValidate from './validate/id';
const router = express.Router();
router.get('/condition/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
ConditionModel.findById(req.params.id).lean().exec((err, data) => {
if (err) return next(err);
if (data) {
res.json(ConditionValidate.output(data));
}
else {
res.status(404).json({status: 'Not found'});
}
});
});
router.put('/condition/' + IdValidate.parameter(), async (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
const {error, value: condition} = ConditionValidate.input(req.body, 'change');
console.log(error);
if (error) return res400(error, res);
const data = await ConditionModel.findById(req.params.id).lean().exec().catch(err => {next(err);}) as any;
if (data instanceof Error) {
return;
}
if (!data) {
res.status(404).json({status: 'Not found'});
}
condition.treatment_template = data.treatment_template;
condition.sample_id = data.sample_id;
if (!await sampleIdCheck(condition, req, res, next)) return;
if (condition.parameters) {
condition.parameters = Object.assign(data.parameters, condition.parameters);
}
if (!await treatmentCheck(condition, 'change', res, next)) return;
console.log(condition);
ConditionModel.findByIdAndUpdate(req.params.id, condition, {new: true}).lean().exec((err, data) => {
if (err) return next(err);
res.json(ConditionValidate.output(data));
});
});
router.delete('/condition/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
ConditionModel.findById(req.params.id).lean().exec(async (err, data: any) => {
if (err) return next(err);
if (!data) {
res.status(404).json({status: 'Not found'});
}
if (!await sampleIdCheck(data, req, res, next)) return;
ConditionModel.findByIdAndDelete(req.params.id).lean().exec(async err => {
if (err) return next(err);
res.json({status: 'OK'});
});
});
});
router.post('/condition/new', async (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
const {error, value: condition} = ConditionValidate.input(req.body, 'new');
if (error) return res400(error, res);
if (!await sampleIdCheck(condition, req, res, next)) return;
if (!await numberCheck(condition, res, next)) return;
if (!await treatmentCheck(condition, 'new', res, next)) return;
new ConditionModel(condition).save((err, data) => {
if (err) return next(err);
res.json(ConditionValidate.output(data.toObject()));
});
})
module.exports = router;
async function sampleIdCheck (condition, req, res, next) { // validate sample_id, returns false if invalid
const sampleData = await SampleModel.findById(condition.sample_id).lean().exec().catch(err => {next(err); return false;}) as any;
if (!sampleData) { // sample_id not found
res.status(400).json({status: 'Sample id not available'});
return false
}
if (sampleData.user_id.toString() !== req.authDetails.id && !req.auth(res, ['maintain', 'admin'], 'basic')) return false; // sample does not belong to user
return true;
}
async function numberCheck (condition, res, next) { // validate number, returns false if invalid
const data = await ConditionModel.find({sample_id: new mongoose.Types.ObjectId(condition.sample_id), number: condition.number}).lean().exec().catch(err => {next(err); return false;}) as any;
if (data.length) {
res.status(400).json({status: 'Condition number already taken'});
return false;
}
return true;
}
async function treatmentCheck (condition, param, res, next) {
const treatmentData = await TreatmentTemplateModel.findById(condition.treatment_template).lean().exec().catch(err => {next(err); return false;}) as any;
if (!treatmentData) { // sample_id not found
res.status(400).json({status: 'Treatment template not available'});
return false
}
// validate parameters
const {error, value: ignore} = ParametersValidate.input(condition.parameters, treatmentData.parameters, param);
console.log(error);
if (error) {res400(error, res); return false;}
return true;
}

View File

@ -153,7 +153,7 @@ describe('/material', () => {
should(res.body).be.eql({_id: '100000000000000000000001', name: 'UltramidTKR4355G7_2', supplier: 'BASF', group: 'PA6/6T', mineral: 0, glass_fiber: 35, carbon_fiber: 0, numbers: [{color: 'black', number: 5514212901}, {color: 'signalviolet', number: 5514612901}]});
MaterialModel.findById('100000000000000000000001').lean().exec((err, data:any) => {
if (err) return done(err);
data._id = data._id.toString({_id: '100000000000000000000001', name: 'UltramidTKR4355G7_2', supplier: 'BASF', group: 'PA6/6T', mineral: 0, glass_fiber: 35, carbon_fiber: 0, numbers: [{color: 'black', number: 5514212901}, {color: 'signalviolet', number: 5514612901}]});
data._id = data._id.toString();
data.numbers = data.numbers.map(e => {return {color: e.color, number: e.number}});
should(data).be.eql({_id: '100000000000000000000001', name: 'UltramidTKR4355G7_2', supplier: 'BASF', group: 'PA6/6T', mineral: '0', glass_fiber: '35', carbon_fiber: '0', numbers: [{color: 'black', number: 5514212901}, {color: 'signalviolet', number: 5514612901}], __v: 0}
);

View File

@ -80,7 +80,7 @@ router.delete('/material/' + IdValidate.parameter(), (req, res, next) => {
MaterialModel.findByIdAndDelete(req.params.id).lean().exec((err, data) => {
if (err) return next(err);
if (data) {
res.json({status: 'OK'})
res.json({status: 'OK'});
}
else {
res.status(404).json({status: 'Not found'});

View File

@ -3,7 +3,7 @@ import SampleModel from '../models/sample';
import NoteModel from '../models/note';
import NoteFieldModel from '../models/note_field';
import TestHelper from "../test/helper";
// TODO: generate sample number
describe('/sample', () => {
let server;

View File

@ -150,7 +150,7 @@ module.exports = router;
async function numberCheck (sample, res, next) { // validate number, returns false if invalid
const sampleData = await SampleModel.findOne({number: sample.number}).lean().exec().catch(err => { return next(err)});
const sampleData = await SampleModel.findOne({number: sample.number}).lean().exec().catch(err => {next(err); return false;});
if (sampleData) { // found entry with sample number
res.status(400).json({status: 'Sample number already taken'});
return false
@ -159,7 +159,7 @@ async function numberCheck (sample, res, next) { // validate number, returns fa
}
async function materialCheck (sample, res, next, id = sample.material_id) { // validate material_id and color, returns false if invalid
const materialData = await MaterialModel.findById(id).lean().exec().catch(err => {next(err);}) as any;
const materialData = await MaterialModel.findById(id).lean().exec().catch(err => {next(err); return false;}) as any;
if (materialData instanceof Error) {
return false;
}

View File

@ -3,7 +3,7 @@ import TemplateTreatmentModel from '../models/treatment_template';
import TemplateMeasurementModel from '../models/measurement_template';
import TestHelper from "../test/helper";
// TODO: remove DELETE methods, only updates possible
describe('/template', () => {
let server;
before(done => TestHelper.before(done));
@ -121,7 +121,7 @@ describe('/template', () => {
TemplateTreatmentModel.find({name: 'heat aging'}).lean().exec((err, data:any) => {
if (err) return done(err);
should(data).have.lengthOf(1);
should(data[0]).have.only.keys('_id', 'name', 'parameters');
should(data[0]).have.only.keys('_id', 'name', 'parameters', '__v');
should(data[0]).have.property('name', 'heat aging');
should(data[0]).have.property('parameters').have.lengthOf(1);
should(data[0].parameters[0]).have.property('name', 'time');
@ -443,7 +443,7 @@ describe('/template', () => {
TemplateMeasurementModel.find({name: 'IR spectrum'}).lean().exec((err, data:any) => {
if (err) return done(err);
should(data).have.lengthOf(1);
should(data[0]).have.only.keys('_id', 'name', 'parameters');
should(data[0]).have.only.keys('_id', 'name', 'parameters', '__v');
should(data[0]).have.property('name', 'IR spectrum');
should(data[0]).have.property('parameters').have.lengthOf(1);
should(data[0].parameters[0]).have.property('name', 'data point table');

View File

@ -0,0 +1,55 @@
import Joi from '@hapi/joi';
import IdValidate from './id';
export default class ConditionValidate {
private static condition = {
sample_id: IdValidate.get(),
number: Joi.string()
.max(128),
parameters: Joi.object()
.pattern(/.*/, Joi.alternatives()
.try(
Joi.string().max(128),
Joi.number(),
Joi.boolean()
)
),
treatment_template: IdValidate.get()
}
static input (data, param) {
if (param === 'new') {
return Joi.object({
sample_id: this.condition.sample_id.required(),
number: this.condition.number.required(),
parameters: this.condition.parameters.required(),
treatment_template: this.condition.treatment_template.required()
}).validate(data);
}
else if (param === 'change') {
return Joi.object({
number: this.condition.number,
parameters: this.condition.parameters
}).validate(data);
}
else {
return{error: 'No parameter specified!', value: {}};
}
}
static output (data) {
data = IdValidate.stringify(data);
const {value, error} = Joi.object({
_id: IdValidate.get(),
sample_id: this.condition.sample_id,
number: this.condition.number,
parameters: this.condition.parameters,
treatment_template: this.condition.treatment_template
}).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}
}

View File

@ -0,0 +1,35 @@
import Joi from '@hapi/joi';
export default class ParametersValidate {
static input (data, parameters, param) { // data to validate, parameters from template, param: 'new', 'change'
let joiObject = {};
parameters.forEach(parameter => {
if (parameter.range.hasOwnProperty('values')) {
joiObject[parameter.name] = Joi.alternatives()
.try(Joi.string(), Joi.number(), Joi.boolean())
.valid(...parameter.range.values);
}
else if (parameter.range.hasOwnProperty('min') && parameter.range.hasOwnProperty('max')) {
joiObject[parameter.name] = Joi.number()
.min(parameter.range.min)
.max(parameter.range.max);
}
else if (parameter.range.hasOwnProperty('min')) {
joiObject[parameter.name] = Joi.number()
.min(parameter.range.min);
}
else if (parameter.range.hasOwnProperty('max')) {
joiObject[parameter.name] = Joi.number()
.max(parameter.range.max);
}
else {
joiObject[parameter.name] = Joi.alternatives()
.try(Joi.string(), Joi.number(), Joi.boolean());
}
if (param === 'new') {
joiObject[parameter.name] = joiObject[parameter.name].required()
}
});
return Joi.object(joiObject).validate(data);
}
}

View File

@ -183,6 +183,52 @@
"__v": 0
}
],
"conditions": [
{
"_id": {"$oid":"700000000000000000000001"},
"sample_id": {"$oid":"400000000000000000000001"},
"number": "B1",
"parameters": {
"material": "copper",
"weeks": 3
},
"treatment_template": {"$oid":"200000000000000000000001"},
"__v": 0
},
{
"_id": {"$oid":"700000000000000000000002"},
"sample_id": {"$oid":"400000000000000000000002"},
"number": "B1",
"parameters": {
"material": "copper",
"weeks": 3
},
"treatment_template": {"$oid":"200000000000000000000001"},
"__v": 0
},
{
"_id": {"$oid":"700000000000000000000003"},
"sample_id": {"$oid":"400000000000000000000004"},
"number": "B1",
"parameters": {
"material": "copper",
"weeks": 3
},
"treatment_template": {"$oid":"200000000000000000000001"},
"__v": 0
},
{
"_id": {"$oid":"700000000000000000000004"},
"sample_id": {"$oid":"400000000000000000000001"},
"number": "B3",
"parameters": {
"material": "hot air",
"weeks": 5
},
"treatment_template": {"$oid":"200000000000000000000001"},
"__v": 0
}
],
"treatment_templates": [
{
"_id": {"$oid":"200000000000000000000001"},
@ -204,7 +250,8 @@
"max": 10
}
}
]
],
"__v": 0
},
{
"_id": {"$oid":"200000000000000000000002"},
@ -214,7 +261,8 @@
"name": "material",
"range": {}
}
]
],
"__v": 0
}
],
"measurement_templates": [
@ -226,7 +274,8 @@
"name": "dpt",
"range": {}
}
]
],
"__v": 0
},
{
"_id": {"$oid":"300000000000000000000002"},
@ -246,7 +295,8 @@
"max": 0.5
}
}
]
],
"__v": 0
}
],
"users": [