|
1 | | -import { difference } from 'lodash'; |
| 1 | +import { difference, isEmpty } from 'lodash'; |
2 | 2 | import express, { Request, Response } from 'express'; |
3 | 3 | import { param, query } from 'express-validator'; |
4 | 4 | import { renderFile } from 'pug'; |
@@ -85,90 +85,107 @@ burialsRouter.get( |
85 | 85 | } |
86 | 86 | ); |
87 | 87 |
|
88 | | -// changed this route from "/new" to "/" to follow RESTFUL conventions |
89 | 88 | burialsRouter.post('/', async (req: Request, res: Response) => { |
90 | | - const { burial = {}, Memberships, SiteVisits, Kinships, Occupations, Sources } = req.body; |
91 | | - |
92 | | - const response = await db |
93 | | - .insert(burial) |
94 | | - .into('Burial.Burial') |
95 | | - .returning('*') |
96 | | - .then(async (rows: any) => { |
| 89 | + const { |
| 90 | + burial = {}, |
| 91 | + Memberships = [], |
| 92 | + SiteVisits = [], |
| 93 | + Kinships = [], |
| 94 | + Occupations = [], |
| 95 | + Sources = [], |
| 96 | + } = req.body; |
| 97 | + |
| 98 | + try { |
| 99 | + const result = await db.transaction(async (trx) => { |
| 100 | + const rows = await trx.insert(burial).into('Burial.Burial').returning('*'); |
97 | 101 | const newBurial = rows[0]; |
98 | | - //OCCUPATIONS |
99 | | - await db |
100 | | - .insert( |
101 | | - Occupations.filter((x: any) => x.new == true && !x.deleted).map((x: any) => ({ |
102 | | - BurialID: newBurial.BurialID, |
103 | | - OccupationID: x.OccupationLupID, |
104 | | - })) |
105 | | - ) |
106 | | - .into('Burial.Occupation') |
107 | | - .then((rows: any) => { |
108 | | - return rows; |
109 | | - }); |
110 | | - //MEMBERSHIPS |
111 | | - await db |
112 | | - .insert( |
113 | | - Memberships.filter((x: any) => x.new == true).map((x: any) => ({ |
114 | | - BurialID: newBurial.BurialID, |
115 | | - MembershipID: x.MembershipLUpID, |
116 | | - Chapter: x.Chapter, |
117 | | - Notes: x.Notes, |
118 | | - })) |
119 | | - ) |
120 | | - .into('Burial.Membership') |
121 | | - .then((rows: any) => { |
122 | | - return rows; |
123 | | - }); |
124 | | - //KINSHIPS |
125 | | - await db |
126 | | - .insert( |
127 | | - Kinships.filter((x: any) => x.new == true).map((x: any) => ({ |
128 | | - BurialID: newBurial.BurialID, |
129 | | - RelationshipID: x.RelationshipID, |
130 | | - Quantity: x.Quantity, |
131 | | - Name: x.Name, |
132 | | - Location: x.Location, |
133 | | - })) |
134 | | - ) |
135 | | - .into('Burial.NOKin') |
136 | | - .then((rows: any) => { |
137 | | - return rows; |
138 | | - }); |
139 | | - //SITE VISITS |
140 | | - await db |
141 | | - .insert( |
142 | | - SiteVisits.filter((x: any) => x.new == true).map((x: any) => ({ |
143 | | - BurialID: newBurial.BurialID, |
144 | | - VisitYear: x.VisitYear, |
145 | | - Condition: x.Condition, |
146 | | - MarkerDescription: x.MarkerDescription, |
147 | | - Inscription: x.Inscription, |
148 | | - RecordedBy: x.RecordedBy, |
149 | | - })) |
150 | | - ) |
151 | | - .into('Burial.SiteVisit') |
152 | | - .then((rows: any) => { |
153 | | - return rows; |
154 | | - }); |
155 | | - //SOURCES |
156 | | - await db |
157 | | - .insert( |
158 | | - Sources.filter((x: any) => x.new == true).map((x: any) => ({ |
159 | | - BurialID: newBurial.BurialID, |
160 | | - Source: x.Source, |
161 | | - })) |
162 | | - ) |
163 | | - .into('Burial.Source') |
164 | | - .then((rows: any) => { |
165 | | - return rows; |
166 | | - }); |
| 102 | + |
| 103 | + if (!isEmpty(Occupations)) { |
| 104 | + await trx |
| 105 | + .insert( |
| 106 | + Occupations.filter((x: any) => x.new == true && !x.deleted).map((x: any) => ({ |
| 107 | + BurialID: newBurial.BurialID, |
| 108 | + OccupationID: x.OccupationLupID, |
| 109 | + })) |
| 110 | + ) |
| 111 | + .into('Burial.Occupation') |
| 112 | + .then((rows: any) => { |
| 113 | + return rows; |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + if (!isEmpty(Memberships)) { |
| 118 | + await trx |
| 119 | + .insert( |
| 120 | + Memberships.filter((x: any) => x.new == true).map((x: any) => ({ |
| 121 | + BurialID: newBurial.BurialID, |
| 122 | + MembershipID: x.MembershipLUpID, |
| 123 | + Chapter: x.Chapter, |
| 124 | + Notes: x.Notes, |
| 125 | + })) |
| 126 | + ) |
| 127 | + .into('Burial.Membership') |
| 128 | + .then((rows: any) => { |
| 129 | + return rows; |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + if (!isEmpty(Kinships)) { |
| 134 | + await trx |
| 135 | + .insert( |
| 136 | + Kinships.filter((x: any) => x.new == true).map((x: any) => ({ |
| 137 | + BurialID: newBurial.BurialID, |
| 138 | + RelationshipID: x.RelationshipID, |
| 139 | + Quantity: x.Quantity, |
| 140 | + Name: x.Name, |
| 141 | + Location: x.Location, |
| 142 | + })) |
| 143 | + ) |
| 144 | + .into('Burial.NOKin') |
| 145 | + .then((rows: any) => { |
| 146 | + return rows; |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + if (!isEmpty(SiteVisits)) { |
| 151 | + await trx |
| 152 | + .insert( |
| 153 | + SiteVisits.filter((x: any) => x.new == true).map((x: any) => ({ |
| 154 | + BurialID: newBurial.BurialID, |
| 155 | + VisitYear: x.VisitYear, |
| 156 | + Condition: x.Condition, |
| 157 | + MarkerDescription: x.MarkerDescription, |
| 158 | + Inscription: x.Inscription, |
| 159 | + RecordedBy: x.RecordedBy, |
| 160 | + })) |
| 161 | + ) |
| 162 | + .into('Burial.SiteVisit') |
| 163 | + .then((rows: any) => { |
| 164 | + return rows; |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + if (!isEmpty(Sources)) { |
| 169 | + await trx |
| 170 | + .insert( |
| 171 | + Sources.filter((x: any) => x.new == true).map((x: any) => ({ |
| 172 | + BurialID: newBurial.BurialID, |
| 173 | + Source: x.Source, |
| 174 | + })) |
| 175 | + ) |
| 176 | + .into('Burial.Source') |
| 177 | + .then((rows: any) => { |
| 178 | + return rows; |
| 179 | + }); |
| 180 | + } |
167 | 181 |
|
168 | 182 | return newBurial; |
169 | 183 | }); |
170 | | - |
171 | | - res.send(response); |
| 184 | + return res.status(201).json(result); |
| 185 | + } catch (error) { |
| 186 | + console.error(error); |
| 187 | + return res.status(422).send({ message: 'Burial creation failed' }); |
| 188 | + } |
172 | 189 | }); |
173 | 190 |
|
174 | 191 | burialsRouter.put( |
|
0 commit comments