admin

package
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CmdAddTimetableEntry = &cobra.Command{
	Use:   "addtimetableentry",
	Short: "Add a timetable entry",
	Long:  `Add a timetable entry for a business primary address`,
	Run: func(cmd *cobra.Command, args []string) {
		ctx := context.TODO()
		uv := privacy.UserViewer{Role: privacy.Admin}
		uv.SetUserID("cli")
		ctx = uv.ToContext(ctx)
		client, clientError := db.CreateClient(ctx)
		if clientError != nil {
			panic(fmt.Sprintf("Error creating client: %v", clientError))
		}
		defer client.Close()
		b, err := client.Business.Query().Where(business.Alias(NewTimetableEntry.BusinessAlias)).WithAddresses(
			func(q *ent.AddressQuery) {
				q.Where(address.PrimaryEQ(true))
			},
		).
			First(ctx)
		if err != nil {
			fmt.Println(fmt.Errorf("failed get business for given alias: %v", err))
			return
		}
		a := b.Edges.Addresses[0]

		from, err := time.Parse(time.DateTime, NewTimetableEntry.From)
		if err != nil {
			fmt.Println("Error parsing from date: ", err)
			return
		}
		if !from.After(time.Now()) {
			fmt.Println("Timetable entry must be in the future: ", from)
			return
		}
		tc := client.Timetable.Create().SetAddress(a).
			SetDatetimeFrom(from).
			SetDuration(NewTimetableEntry.Duration)
		var tttype timetable.TimetableType
		switch strings.ToUpper(NewTimetableEntry.TTType) {
		case timetable.TimetableTypeEMERGENCYSERVICE.String():
			tttype = timetable.TimetableTypeEMERGENCYSERVICE
		case timetable.TimetableTypeCLOSED.String():
			tttype = timetable.TimetableTypeCLOSED
		case timetable.TimetableTypeHOLIDAY.String():
			tttype = timetable.TimetableTypeHOLIDAY
		case timetable.TimetableTypeSPECIAL.String():
			tttype = timetable.TimetableTypeSPECIAL
		case timetable.TimetableTypeREGULAR.String():
			tttype = timetable.TimetableTypeREGULAR
		default:
			tttype = timetable.TimetableTypeDEFAULT
		}

		c, err := client.Timetable.Query().Where(timetable.TimetableTypeEQ(tttype)).
			Where(timetable.HasAddressWith(address.IDEQ(a.ID))).
			Where(timetable.DatetimeFromEQ(from)).Count(ctx)
		if err != nil {
			fmt.Println(fmt.Errorf("failed to check for existing timetable entry: %v", err))
			return
		} else if c > 0 {
			fmt.Println(fmt.Errorf("timetable entry already exists"))
			return
		}
		tc.Mutation().SetTimetableType(tttype)
		err = tc.Exec(ctx)
		if err != nil {
			fmt.Println(fmt.Errorf("failed to create timetable entry: %v", err))
		}
		fmt.Println("Timetable entry created: ")

	},
}
View Source
var CmdBusiness = &cobra.Command{
	Use:   "business",
	Short: "Manage Businesses",
	Long:  `Manage Businesses, e.g. View, Create and Update them`,
}
View Source
var CmdCreateUser = &cobra.Command{
	Use:   "create",
	Short: "Create a new user",
	Long:  `Create a new user`,
	Run: func(cmd *cobra.Command, args []string) {
		ctx := context.TODO()
		uv := privacy.UserViewer{Role: privacy.Admin}
		uv.SetUserID("cli")
		ctx = uv.ToContext(ctx)
		client, clientError := db.CreateClient(ctx)
		if clientError != nil {
			panic(fmt.Sprintf("Error creating client: %v", clientError))
		}
		defer client.Close()
		pwh, err := utils.HashPassword(NewUser.Password)
		if err != nil {
			fmt.Println(fmt.Errorf("failed hashing password: %v", err))
			os.Exit(1)
		}

		u, err := client.User.Create().
			SetLogin(NewUser.Login).
			SetEmail(NewUser.EMail).
			SetFirstname(NewUser.Firstname).
			SetSurname(NewUser.Surname).
			SetPasswordhash(pwh).SetRole(fmt.Sprint(NewUser.Role)).
			Save(ctx)
		if err != nil {
			fmt.Println(fmt.Errorf("failed creating user: %v", err))
			os.Exit(1)
		}
		fmt.Println("User created with ID: ", u.ID)
		if NewUser.Scopes != nil && len(NewUser.Scopes) > 0 {
			for _, scope := range NewUser.Scopes {
				id := uuid.Must(uuid.Parse(scope))
				bq := client.Business.Query().Where(business.IDEQ(id)).WithUsers()
				b, err := bq.First(ctx)
				if err != nil {
					fmt.Println(fmt.Errorf("cannot get sccope: %v", err))
					os.Exit(1)
				}
				err = b.Update().AddUsers(u).Exec(ctx)
				if err != nil {
					fmt.Println(fmt.Errorf("cannot add user scope to business: %v", err))
					os.Exit(1)
				}
				fmt.Println("scope added: ", b.ID)
			}
			if NewUser.PublicAPI == "1" {
				err = u.Update().SetUsePublicapi("1").Exec(ctx)
				if err != nil {
					fmt.Println(fmt.Errorf("failed setting public api: %v", err))
					os.Exit(1)
				}
			}

		}
	},
}
View Source
var CmdListBusiness = &cobra.Command{
	Use:   "list",
	Short: "List all businesses",
	Long:  `List all businesses`,
	Run: func(cmd *cobra.Command, args []string) {
		ctx := context.TODO()
		uv := privacy.UserViewer{Role: privacy.Admin}
		uv.SetUserID("cli")
		ctx = uv.ToContext(ctx)
		client, clientError := db.CreateClient(ctx)
		if clientError != nil {
			panic(fmt.Sprintf("Error creating client: %v", clientError))
		}
		defer client.Close()
		b, err := client.Business.Query().All(ctx)
		if err != nil {
			fmt.Println(fmt.Errorf("failed listing businesses: %v", err))
		}

		table := tablewriter.NewWriter(os.Stdout)
		table.SetHeader([]string{"ID", "Name1", "Name2", "Alias", "Telephone", "Active", "Updated"})

		for _, v := range b {
			table.Append([]string{
				v.ID.String(),
				v.Name1,
				*v.Name2,
				v.Alias,
				*v.Telephone,
				strconv.FormatBool(v.Active),
				v.UpdatedAt.Format(time.DateTime),
			})
		}
		table.Render()
	},
}
View Source
var CmdUser = &cobra.Command{
	Use:   "user",
	Short: "Manage users",
	Long:  `Manage users`,
}
View Source
var NewTimetableEntry = newTimetableEntry{}
View Source
var NewUser = newUser{}
View Source
var RootCmd = &cobra.Command{
	Use:   "admin",
	Short: "Manage various aspects of the application",
	Long:  `With admin you can manage the users (e.g. create/delete).`,
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL