Skip to content

Commit f4bbe06

Browse files
authored
fix: adds equipment assets that have no parents (#53)
Adds equipment with no parents to facilitate testing related to [FarmData2 #298](FarmData2/FarmData2#298)
1 parent d3f5065 commit f4bbe06

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

dist/db.sample.tar.gz

20.5 KB
Binary file not shown.

src/sampleDB/addEquipment.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as farmosUtil from "../library/farmosUtil/farmosUtil.js";
33

44
import { basename, dirname } from "path";
55
import { fileURLToPath } from "url";
6-
import { LocalStorage } from 'node-localstorage';
6+
import { LocalStorage } from "node-localstorage";
77

88
/*
99
* Set the name of the CSV file to be processed and the
@@ -28,7 +28,7 @@ const pass = "admin";
2828
* Get a local storage object that we'll use to simulate the
2929
* browser's localStorage and sessionStorage when running in node.
3030
*/
31-
let ls = new LocalStorage('scratch');
31+
let ls = new LocalStorage("scratch");
3232

3333
/*
3434
* Get a fully initialized and logged in instance of the farmOS.js
@@ -61,27 +61,56 @@ let categoryName = null;
6161
*/
6262
async function processRow(row) {
6363
if (row[0] != "") {
64-
console.log(" Adding " + row[0] + "...");
65-
const categoryParent = farm.asset.create({
66-
type: "asset--equipment",
67-
attributes: {
68-
name: row[0],
69-
notes: row[1],
70-
},
71-
});
64+
let equipment = null;
65+
if (row.length == 1) {
66+
console.log(" Adding equipment " + row[0] + ", which has no parents...");
67+
equipment = farm.asset.create({
68+
type: "asset--equipment",
69+
attributes: {
70+
name: row[0],
71+
},
72+
});
73+
} else if (row.length == 4) {
74+
console.log(" Adding equipment " + row[0] + ", which has no parents...");
75+
equipment = farm.asset.create({
76+
type: "asset--equipment",
77+
attributes: {
78+
name: row[0],
79+
manufacturer: row[1],
80+
model: row[2],
81+
notes: row[3],
82+
},
83+
});
84+
} else {
85+
console.log(" Adding " + row[0] + "...");
86+
equipment = farm.asset.create({
87+
type: "asset--equipment",
88+
attributes: {
89+
name: row[0],
90+
notes: row[1],
91+
},
92+
});
93+
categoryParentId = equipment.id;
94+
categoryParentName = row[0];
95+
}
7296

7397
try {
74-
const result = await farm.asset.send(categoryParent);
75-
categoryParentId = result.id;
76-
categoryParentName = row[0];
98+
const result = await farm.asset.send(equipment);
7799
} catch (e) {
78100
console.log("API error sending " + row[0]);
79101
console.log(e);
80102
process.exit(1);
81103
}
82104
console.log(" Added.");
105+
83106
} else if (row[1] != "") {
84-
console.log(" Adding equipment category " + row[1] + " to category " + categoryParentName + "...");
107+
console.log(
108+
" Adding equipment category " +
109+
row[1] +
110+
" to category " +
111+
categoryParentName +
112+
"..."
113+
);
85114
const category = farm.asset.create({
86115
type: "asset--equipment",
87116
attributes: {

src/sampleDB/sampleData/equipment.csv

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
# Sample Data for the equipment assets
22
#
3-
# Format:
4-
#
53
# Each line represents either a category of equipment or
64
# any equipment asset.
75
#
6+
# Format:
7+
#
8+
# A left justified line creates an equipment asset with no parent
9+
# (e.g. Rake or Shovel) or it creates 'Category' which is the parent
10+
# of all categories of equipment
11+
#
12+
# A line beginning with a single comma:
13+
# Creates an equipment category with its parent being 'Category' that it
14+
# appears under.
15+
#
16+
# A line beginning with two commas:
17+
# Creates an equipment asset and sets its parent to the equipment
18+
# category that it appears under (i.e. General is the parent of Tractor)
19+
#
820
# Equipment categories have the format:
921
# category name,category description.
1022
#
11-
# Equipment within a category are listed immediately following its category
12-
# and have the following comma delimited information:
13-
# ,name,manufacturer,model,description
23+
# Equipment assets have the following comma delimited information:
24+
# name,manufacturer,model,description
1425
#
1526
# Anything following a # on a line is a considered a comment.
1627
# Thus, names and descriptions cannot contain #
1728
# Blank Lines are ignored.
1829

19-
Category, Parent of all categories
30+
Rake,Company A,9871,A garden rake.
31+
Shovel,
32+
Category,Parent of all categories
2033
,General,Equipment used for a variety of operations.
2134
,,Tractor,Company G,468,A standard tractor.
2235
,,Small Tractor,Company I,987,A compact tractor.

0 commit comments

Comments
 (0)