2022-10-12 20:53:22 +00:00
package main
import (
"context"
"strings"
2024-03-05 15:44:43 +00:00
"time"
2022-10-12 20:53:22 +00:00
2022-10-30 04:05:38 +00:00
"github.com/hay-kot/homebox/backend/internal/core/services"
2022-10-12 20:53:22 +00:00
"github.com/rs/zerolog/log"
)
func ( a * app ) SetupDemo ( ) {
2023-02-26 02:54:40 +00:00
csvText := ` HB . import_ref , HB . location , HB . labels , HB . quantity , HB . name , HB . description , HB . insured , HB . serial_number , HB . model_number , HB . manufacturer , HB . notes , HB . purchase_from , HB . purchase_price , HB . purchase_time , HB . lifetime_warranty , HB . warranty_expires , HB . warranty_details , HB . sold_to , HB . sold_price , HB . sold_time , HB . sold_notes
2022-10-12 20:53:22 +00:00
, Garage , IOT ; Home Assistant ; Z - Wave , 1 , Zooz Universal Relay ZEN17 , "Zooz 700 Series Z-Wave Universal Relay ZEN17 for Awnings, Garage Doors, Sprinklers, and More | 2 NO-C-NC Relays (20A, 10A) | Signal Repeater | Hub Required (Compatible with SmartThings and Hubitat)" , , , ZEN17 , Zooz , , Amazon , 39.95 , 10 / 13 / 2021 , , , , , , ,
, Living Room , IOT ; Home Assistant ; Z - Wave , 1 , Zooz Motion Sensor , "Zooz Z-Wave Plus S2 Motion Sensor ZSE18 with Magnetic Mount, Works with Vera and SmartThings" , , , ZSE18 , Zooz , , Amazon , 29.95 , 10 / 15 / 2021 , , , , , , ,
, Office , IOT ; Home Assistant ; Z - Wave , 1 , Zooz 110 v Power Switch , "Zooz Z-Wave Plus Power Switch ZEN15 for 110V AC Units, Sump Pumps, Humidifiers, and More" , , , ZEN15 , Zooz , , Amazon , 39.95 , 10 / 13 / 2021 , , , , , , ,
, Downstairs , IOT ; Home Assistant ; Z - Wave , 1 , Ecolink Z - Wave PIR Motion Sensor , "Ecolink Z-Wave PIR Motion Detector Pet Immune, White (PIRZWAVE2.5-ECO)" , , , PIRZWAVE2 .5 - ECO , Ecolink , , Amazon , 35.58 , 10 / 21 / 2020 , , , , , , ,
, Entry , IOT ; Home Assistant ; Z - Wave , 1 , Yale Security Touchscreen Deadbolt , "Yale Security YRD226-ZW2-619 YRD226ZW2619 Touchscreen Deadbolt, Satin Nickel" , , , YRD226ZW2619 , Yale , , Amazon , 120.39 , 10 / 14 / 2020 , , , , , , ,
2024-01-04 17:55:26 +00:00
, Kitchen , IOT ; Home Assistant ; Z - Wave , 1 , Smart Rocker Light Dimmer , "UltraPro Z-Wave Smart Rocker Light Dimmer with QuickFit and SimpleWire, 3-Way Ready, Compatible with Alexa, Google Assistant, ZWave Hub Required, Repeater/Range Extender, White Paddle Only, 39351" , , , 39351 , Honeywell , , Amazon , 65.98 , 0 9 / 30 / 0202 , , , , , , ,
2022-10-12 20:53:22 +00:00
`
2024-03-05 15:44:43 +00:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , 10 * time . Second )
defer cancel ( )
2023-02-26 02:54:40 +00:00
registration := services . UserRegistration {
Email : "demo@example.com" ,
Name : "Demo" ,
Password : "demo" ,
}
2022-10-12 20:53:22 +00:00
// First check if we've already setup a demo user and skip if so
2024-03-05 15:44:43 +00:00
log . Debug ( ) . Msg ( "Checking if demo user already exists" )
_ , err := a . services . User . Login ( ctx , registration . Email , registration . Password , false )
2022-10-12 20:53:22 +00:00
if err == nil {
2024-03-05 15:44:43 +00:00
log . Info ( ) . Msg ( "Demo user already exists, skipping setup" )
2022-10-12 20:53:22 +00:00
return
}
2024-03-05 15:44:43 +00:00
log . Debug ( ) . Msg ( "Demo user does not exist, setting up demo" )
_ , err = a . services . User . RegisterUser ( ctx , registration )
2022-10-12 20:53:22 +00:00
if err != nil {
log . Err ( err ) . Msg ( "Failed to register demo user" )
log . Fatal ( ) . Msg ( "Failed to setup demo" )
}
2024-03-05 15:44:43 +00:00
token , err := a . services . User . Login ( ctx , registration . Email , registration . Password , false )
if err != nil {
log . Err ( err ) . Msg ( "Failed to login demo user" )
log . Fatal ( ) . Msg ( "Failed to setup demo" )
return
}
self , err := a . services . User . GetSelf ( ctx , token . Raw )
if err != nil {
log . Err ( err ) . Msg ( "Failed to get self" )
log . Fatal ( ) . Msg ( "Failed to setup demo" )
return
}
2022-10-12 20:53:22 +00:00
2024-03-05 15:44:43 +00:00
_ , err = a . services . Items . CsvImport ( ctx , self . GroupID , strings . NewReader ( csvText ) )
2022-10-12 20:53:22 +00:00
if err != nil {
log . Err ( err ) . Msg ( "Failed to import CSV" )
log . Fatal ( ) . Msg ( "Failed to setup demo" )
}
log . Info ( ) . Msg ( "Demo setup complete" )
}