diff --git a/api/schemas.yaml b/api/schemas.yaml index e76cfb0..c4814b7 100644 --- a/api/schemas.yaml +++ b/api/schemas.yaml @@ -165,14 +165,6 @@ Template: min: 0 max: 2 -ConditionTemplate: - allOf: - - $ref: 'api.yaml#/components/schemas/Template' - properties: - number_prefix: - type: string - example: B - Email: properties: email: diff --git a/api/template.yaml b/api/template.yaml index 71a282f..4fa938d 100644 --- a/api/template.yaml +++ b/api/template.yaml @@ -14,7 +14,7 @@ schema: type: array items: - $ref: 'api.yaml#/components/schemas/ConditionTemplate' + $ref: 'api.yaml#/components/schemas/Template' 401: $ref: 'api.yaml#/components/responses/401' 500: @@ -36,7 +36,7 @@ content: application/json: schema: - $ref: 'api.yaml#/components/schemas/ConditionTemplate' + $ref: 'api.yaml#/components/schemas/Template' 401: $ref: 'api.yaml#/components/responses/401' 404: @@ -56,14 +56,14 @@ content: application/json: schema: - $ref: 'api.yaml#/components/schemas/ConditionTemplate' + $ref: 'api.yaml#/components/schemas/Template' responses: 200: description: condition details content: application/json: schema: - $ref: 'api.yaml#/components/schemas/ConditionTemplate' + $ref: 'api.yaml#/components/schemas/Template' 400: $ref: 'api.yaml#/components/responses/400' 401: @@ -88,14 +88,14 @@ content: application/json: schema: - $ref: 'api.yaml#/components/schemas/ConditionTemplate' + $ref: 'api.yaml#/components/schemas/Template' responses: 200: description: condition details content: application/json: schema: - $ref: 'api.yaml#/components/schemas/ConditionTemplate' + $ref: 'api.yaml#/components/schemas/Template' 400: $ref: 'api.yaml#/components/responses/400' 401: diff --git a/src/models/condition_template.ts b/src/models/condition_template.ts index 20c7234..62bf621 100644 --- a/src/models/condition_template.ts +++ b/src/models/condition_template.ts @@ -1,6 +1,7 @@ import mongoose from 'mongoose'; const ConditionTemplateSchema = new mongoose.Schema({ + first_id: mongoose.Schema.Types.ObjectId, name: String, version: Number, parameters: [{ diff --git a/src/models/measurement_template.ts b/src/models/measurement_template.ts index 080f42b..af14fee 100644 --- a/src/models/measurement_template.ts +++ b/src/models/measurement_template.ts @@ -1,6 +1,7 @@ import mongoose from 'mongoose'; const MeasurementTemplateSchema = new mongoose.Schema({ + first_id: mongoose.Schema.Types.ObjectId, name: String, version: Number, parameters: [{ diff --git a/src/routes/template.spec.ts b/src/routes/template.spec.ts index 95141af..e69b480 100644 --- a/src/routes/template.spec.ts +++ b/src/routes/template.spec.ts @@ -133,7 +133,8 @@ describe('/template', () => { if (err) return done(err); TemplateConditionModel.findById(res.body._id).lean().exec((err, data:any) => { if (err) return done(err); - should(data).have.only.keys('_id', 'name', 'version', 'parameters', '__v'); + should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); + should(data.first_id.toString()).be.eql('200000000000000000000001'); should(data).have.property('name', 'heat aging'); should(data).have.property('version', 2); should(data).have.property('parameters').have.lengthOf(1); @@ -155,7 +156,8 @@ describe('/template', () => { if (err) return done(err); TemplateConditionModel.findById(res.body._id).lean().exec((err, data:any) => { if (err) return done(err); - should(data).have.only.keys('_id', 'name', 'version', 'parameters', '__v'); + should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); + should(data.first_id.toString()).be.eql('200000000000000000000001'); should(data).have.property('name', 'heat aging'); should(data).have.property('version', 2); should(data).have.property('parameters').have.lengthOf(2); @@ -315,7 +317,8 @@ describe('/template', () => { if (err) return done(err); TemplateConditionModel.findById(res.body._id).lean().exec((err, data:any) => { if (err) return done(err); - should(data).have.only.keys('_id', 'name', 'version', 'parameters', '__v'); + should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); + should(data.first_id.toString()).be.eql(data._id.toString()); should(data).have.property('name', 'heat aging'); should(data).have.property('version', 1); should(data).have.property('parameters').have.lengthOf(1); @@ -556,7 +559,8 @@ describe('/template', () => { should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]}); TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => { if (err) return done(err); - should(data).have.only.keys('_id', 'name', 'version', 'parameters', '__v'); + should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); + should(data.first_id.toString()).be.eql('300000000000000000000001'); should(data).have.property('name', 'IR spectrum'); should(data).have.property('version', 2); should(data).have.property('parameters').have.lengthOf(1); @@ -580,7 +584,8 @@ describe('/template', () => { should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, parameters: [{name: 'dpt', range: {type: 'array'}}]}); TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => { if (err) return done(err); - should(data).have.only.keys('_id', 'name', 'version', 'parameters', '__v'); + should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); + should(data.first_id.toString()).be.eql('300000000000000000000001'); should(data).have.property('name', 'IR spectrum'); should(data).have.property('version', 2); should(data).have.property('parameters').have.lengthOf(1); @@ -731,7 +736,8 @@ describe('/template', () => { TemplateMeasurementModel.find({name: 'vz'}).lean().exec((err, data:any) => { if (err) return done(err); should(data).have.lengthOf(1); - should(data[0]).have.only.keys('_id', 'name', 'version', 'parameters', '__v'); + should(data[0]).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v'); + should(data[0].first_id.toString()).be.eql(data[0]._id.toString()); should(data[0]).have.property('name', 'vz'); should(data[0]).have.property('version', 1); should(data[0]).have.property('parameters').have.lengthOf(1); diff --git a/src/routes/template.ts b/src/routes/template.ts index 849cf59..2873946 100644 --- a/src/routes/template.ts +++ b/src/routes/template.ts @@ -6,6 +6,7 @@ import ConditionTemplateModel from '../models/condition_template'; import MeasurementTemplateModel from '../models/measurement_template'; import res400 from './validate/res400'; import IdValidate from './validate/id'; +import mongoose from "mongoose"; @@ -65,6 +66,8 @@ router.post('/template/:collection(measurement|condition)/new', async (req, res, const {error, value: template} = TemplateValidate.input(req.body, 'new'); if (error) return res400(error, res); + template._id = mongoose.Types.ObjectId(); // set reference to itself for first version of template + template.first_id = template._id; template.version = 1; // set template version await new (model(req))(template).save((err, data) => { if (err) next (err); diff --git a/src/test/db.json b/src/test/db.json index 0d49876..4ef811c 100644 --- a/src/test/db.json +++ b/src/test/db.json @@ -423,6 +423,7 @@ "condition_templates": [ { "_id": {"$oid":"200000000000000000000001"}, + "first_id": {"$oid":"200000000000000000000001"}, "name": "heat treatment", "version": 1, "parameters": [ @@ -447,6 +448,7 @@ }, { "_id": {"$oid":"200000000000000000000002"}, + "first_id": {"$oid":"200000000000000000000001"}, "name": "heat treatment 2", "version": 2, "parameters": [ @@ -459,6 +461,7 @@ }, { "_id": {"$oid":"200000000000000000000003"}, + "first_id": {"$oid":"200000000000000000000003"}, "name": "raw material", "version": 1, "parameters": [ @@ -469,6 +472,7 @@ "measurement_templates": [ { "_id": {"$oid":"300000000000000000000001"}, + "first_id": {"$oid":"300000000000000000000001"}, "name": "spectrum", "version": 1, "parameters": [ @@ -483,6 +487,7 @@ }, { "_id": {"$oid":"300000000000000000000002"}, + "first_id": {"$oid":"300000000000000000000001"}, "name": "kf", "version": 2, "parameters": [ @@ -505,6 +510,7 @@ }, { "_id": {"$oid":"300000000000000000000003"}, + "first_id": {"$oid":"300000000000000000000003"}, "name": "mt 3", "version": 1, "parameters": [