Skip to content

Commit

Permalink
fix import BOM
Browse files Browse the repository at this point in the history
  • Loading branch information
phcreery committed Mar 26, 2023
1 parent a59c96c commit 8124862
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 84 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func main() {

server.AddImportComponentsRoute(app, e)

server.AddImportProjectComponentsRoute(app, e)

bindStaticAdminUI(app, e)

return nil
Expand Down
196 changes: 129 additions & 67 deletions server/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ import (
"github.com/pocketbase/pocketbase/models"
)

func GetRelationID(app core.App, collection_name string, relation_name string, relation_value string) (string, error) {
// relation_name is the name OR id of the field in the collection
// search for the record by id
record, err := app.Dao().FindRecordById(collection_name, relation_value)
// if not found search for the record by name
if err != nil {
record, err = app.Dao().FindFirstRecordByData(collection_name, relation_name, relation_value)
if err != nil {
// if still not found, create a new record
collection, err := app.Dao().FindCollectionByNameOrId(collection_name)
if err != nil {
return "", err
}
record = models.NewRecord(collection)
record.Set(relation_name, relation_value)
if err := app.Dao().SaveRecord(record); err != nil {
return "", err
}
// find the category again
record, err = app.Dao().FindFirstRecordByData(collection_name, relation_name, relation_value)
if err != nil {
return "", err
}
}
}
return record.Id, nil
}

func AddImportComponentsRoute(app core.App, e *core.ServeEvent) {

type ImportComponent struct {
Expand Down Expand Up @@ -59,99 +87,133 @@ func AddImportComponentsRoute(app core.App, e *core.ServeEvent) {
return apis.NewNotFoundError("cant find record", err)
}
}
// record, err := app.Dao().FindRecordById("articles", "RECORD_ID")
record.Set("mpn", component.MPN)
record.Set("manufacturer", component.Manufacturer)
record.Set("description", component.Description)
record.Set("stock", component.Stock)
record.Set("ipn", component.IPN)

// search for the category and set the category id
category_record, err := app.Dao().FindRecordById("component_categories", component.Category)
category_record_id, err := GetRelationID(app, "component_categories", "name", component.Category)
if err != nil {
category_record, err = app.Dao().FindFirstRecordByData("component_categories", "name", component.Category)
if err != nil {
// return apis.NewNotFoundError("cant find category", err)
// create a new category
category_collection, err := app.Dao().FindCollectionByNameOrId("component_categories")
if err != nil {
return apis.NewNotFoundError("cant find collection component_categories", err)
}
category_record = models.NewRecord(category_collection)
category_record.Set("name", component.Category)
if err := app.Dao().SaveRecord(category_record); err != nil {
return apis.NewBadRequestError("cant save category record", err)
}
// find the category again
category_record, err = app.Dao().FindFirstRecordByData("component_categories", "name", component.Category)
if err != nil {
return apis.NewNotFoundError("cant find category", err)
}
}
return apis.NewNotFoundError("cant find category", err)
}
record.Set("category", category_record.Id)
record.Set("category", category_record_id)

// search for the footprint and set the footprint id
footprint_record, err := app.Dao().FindRecordById("footprints", component.Footprint)
footprint_record_id, err := GetRelationID(app, "footprints", "name", component.Footprint)
if err != nil {
footprint_record, err = app.Dao().FindFirstRecordByData("footprints", "name", component.Footprint)
if err != nil {
// return apis.NewNotFoundError("cant find footprint", err)
// create a new footprint
footprint_collection, err := app.Dao().FindCollectionByNameOrId("footprints")
if err != nil {
return apis.NewNotFoundError("cant find collection footprints", err)
}
footprint_record = models.NewRecord(footprint_collection)
footprint_record.Set("name", component.Footprint)
if err := app.Dao().SaveRecord(footprint_record); err != nil {
return apis.NewBadRequestError("cant save footprint record", err)
}
// find the footprint again
footprint_record, err = app.Dao().FindFirstRecordByData("footprints", "name", component.Footprint)
if err != nil {
return apis.NewNotFoundError("cant find footprint", err)
}
}
return apis.NewNotFoundError("cant find footprint", err)
}
record.Set("footprint", footprint_record.Id)
record.Set("footprint", footprint_record_id)

// search for the storage location and set the storage location id
storage_location_record, err := app.Dao().FindRecordById("storage_locations", component.Storage_location)
storage_location_record_id, err := GetRelationID(app, "storage_locations", "name", component.Storage_location)
if err != nil {
storage_location_record, err = app.Dao().FindFirstRecordByData("storage_locations", "name", component.Storage_location)
return apis.NewNotFoundError("cant find storage_location", err)
}
record.Set("storage_location", storage_location_record_id)

if err := app.Dao().SaveRecord(record); err != nil {
return apis.NewBadRequestError("cant save record", err)
}

}

return c.String(http.StatusOK, "ok")
},
Middlewares: []echo.MiddlewareFunc{
// apis.RequireAdminOrUserAuth(),
apis.RequireRecordAuth(),
},
})
}

func AddImportProjectComponentsRoute(app core.App, e *core.ServeEvent) {

// { prop: "bom_id", label: "BOM ID", mergeOptions: { single: true, required: true } },
// { prop: "component", label: "MPN", uniqueKey: "mpn", mergeOptions: { single: true } },
// { prop: "quantity", label: "Quantity", mergeOptions: { required: true, defaultBoth: true } },
// { prop: "refdesignators", label: "Ref. Designators", mergeOptions: { defaultBoth: true } },
// { prop: "comment", label: "Comment", mergeOptions: { defaultBoth: true } }

type ImportComponent struct {
Action string `json:"action"`
ID string `json:"id"`
Project_ID string `json:"project_id"`
BOM_ID string `json:"bom_id"`
Component string `json:"component"`
Quantity int `json:"quantity"`
Refdesignators string `json:"refdesignators"`
Comment string `json:"comment"`
}

type ImportComponents struct {
Data []ImportComponent `json:"data"`
}

e.Router.AddRoute(echo.Route{
Method: http.MethodPost,
Path: "/api/custom/importprojectcomponents",
Handler: func(c echo.Context) error {

var json_map ImportComponents
err := json.NewDecoder(c.Request().Body).Decode(&json_map)
if err != nil {
return apis.NewBadRequestError("cant decode body (import data)", err)
}
data := json_map.Data

// loop through the data and create a new component record for each

collection, err := app.Dao().FindCollectionByNameOrId("project_components")
if err != nil {
return apis.NewNotFoundError("cant find collection project_components", err)
}

for _, component := range data {
fmt.Println(component)
record := models.NewRecord(collection)
// if component Action is "update" then find the record and update it
if component.Action == "update" {
record, err = app.Dao().FindRecordById("components", component.ID)
if err != nil {
// return apis.NewNotFoundError("cant find storage location", err)
// create a new storage location
storage_location_collection, err := app.Dao().FindCollectionByNameOrId("storage_locations")
if err != nil {
return apis.NewNotFoundError("cant find collection storage_locations", err)
}
storage_location_record = models.NewRecord(storage_location_collection)
storage_location_record.Set("name", component.Storage_location)
if err := app.Dao().SaveRecord(storage_location_record); err != nil {
return apis.NewBadRequestError("cant save storage location record", err)
}
// find the storage location again
storage_location_record, err = app.Dao().FindFirstRecordByData("storage_locations", "name", component.Storage_location)
if err != nil {
return apis.NewNotFoundError("cant find storage location", err)
}
return apis.NewNotFoundError("cant find record", err)
}
}
record.Set("storage_location", storage_location_record.Id)
record.Set("bom_id", component.BOM_ID)
record.Set("quantity", component.Quantity)
record.Set("refdesignators", component.Refdesignators)
record.Set("comment", component.Comment)

component_record_id, err := GetRelationID(app, "components", "mpn", component.Component)
if err != nil {
return apis.NewNotFoundError("cant find component", err)
}
record.Set("component", component_record_id)

if err := app.Dao().SaveRecord(record); err != nil {
// return err
return apis.NewBadRequestError("cant save record", err)
}

// find the project_component again
record, err = app.Dao().FindFirstRecordByData("project_components", "bom_id", component.BOM_ID)
if err != nil {
return apis.NewBadRequestError("cant find newly created record", err)
}
// find the project
project_record, err := app.Dao().FindFirstRecordByData("projects", "id", component.Project_ID)
if err != nil {
return apis.NewBadRequestError("cant find project", err)
}
// append the project_component ID to the project "components" relation
project_record.Set("components", append(project_record.Get("components").([]string), record.Id))
if err := app.Dao().SaveRecord(project_record); err != nil {
return apis.NewBadRequestError("cant save newly updated project record", err)
}
}

return c.String(http.StatusOK, "ok")
},
Middlewares: []echo.MiddlewareFunc{
// apis.RequireAdminOrUserAuth(),
apis.RequireRecordAuth(),
},
})
}
8 changes: 8 additions & 0 deletions src/api/modules/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ export const deleteProjectComponents = async (params: ProjectComponents.ReqRemov
return { data: record } as unknown as APIdata<Project.ResGetProjectRecord>;
};

export const postProjectComponentsUpload = async (params: any) => {
let res = await client.send("/api/custom/importprojectcomponents", {
method: "POST",
body: { data: params }
});
return { data: res } as unknown;
};

// ---- PROJECT BUILDS ----

export const getProjectBuildsList = async (params: ProjectBuilds.ReqGetProjectBuildListParams) => {
Expand Down
27 changes: 10 additions & 17 deletions src/views/projects/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ import { CirclePlus, Delete, EditPen, Upload, Download } from "@element-plus/ico
import { ResList, Project, ProjectComponents } from "@/api/interface";
import {
getComponentEnum,
postComponentCreate,
// postComponentCreate,
getProjectsEnum,
getProjectComponentsList,
getProjectComponentsListForExport,
postProjectComponentsUpload,
postProjectComponentAdd,
patchProjectComponentUpdate,
deleteProjectComponents,
Expand Down Expand Up @@ -235,15 +236,6 @@ interface DialogExpose {
acceptParams: (params: any) => void;
}
const dialogRefImport = ref<DialogExpose>();
// const batchAdd = () => {
// let params = {
// title: "component",
// // tempApi: exportUserInfo,
// // importApi: BatchAddUser,
// getTableList: proTable.value.getTableList
// };
// dialogRef.value!.acceptParams(params);
// };
const batchAdd = async () => {
if (!initParam.projectID) {
ElNotification({
Expand All @@ -255,23 +247,24 @@ const batchAdd = async () => {
}
let templateColumns = [
{ prop: "bom_id", label: "BOM ID", mergeOptions: { single: true, required: true } },
{ prop: "component", label: "MPN", apiCreate: postComponentCreate, uniqueKey: "mpn", mergeOptions: { single: true } },
{ prop: "component", label: "MPN", uniqueKey: "mpn", mergeOptions: { single: true } },
{ prop: "quantity", label: "Quantity", mergeOptions: { required: true, defaultBoth: true } },
{ prop: "refdesignators", label: "Ref. Designators", mergeOptions: { defaultBoth: true } },
{ prop: "comment", label: "Comment", mergeOptions: { defaultBoth: true } }
];
console.log(templateColumns);
console.log(proTable.value.enumMap);
let params = {
title: "Project Components",
columns: templateColumns,
uniqueKey: "bom_id",
enumMap: proTable.value.enumMap,
apiGetExistingEntries: async () => await getProjectComponentsListForExport({ filter: {}, projectID: initParam.projectID! }), // existingEntries,
apiCreate: (params: ProjectComponents.ReqAddProjectComponentParams) =>
postProjectComponentAdd({ ...params, _ofProjectID: initParam.projectID! }),
apiUpdate: (params: ProjectComponents.ReqUpdateProjectComponentParams) =>
patchProjectComponentUpdate({ ...params, _ofProjectID: initParam.projectID! }),
apiUpload: async (data: any) => {
// add project_id to each row
data.forEach((row: any) => {
row.project_id = initParam.projectID;
});
await postProjectComponentsUpload(data);
},
refresh: proTable.value.getTableList
};
dialogRefImport.value!.acceptParams(params);
Expand Down

0 comments on commit 8124862

Please sign in to comment.