55 "encoding/json"
66 "errors"
77 "fmt"
8+ "log"
89 "net/http"
910 "os"
1011
@@ -19,23 +20,6 @@ import (
1920
2021var temp int
2122
22- type Schema struct {
23- Display string `json:"Display"`
24- Enabled bool `json:"Enabled"`
25- IsMandatory bool `json:"IsMandatory"`
26- Parent string `json:"Parent"`
27- ParentDataSource string `json:"ParentDataSource"`
28- Permission string `json:"Permission"`
29- Name string `json:"name"`
30- Rules string `json:"rules"`
31- Status string `json:"status"`
32- Type string `json:"type"`
33- }
34- type schemaStr struct {
35- Data []Schema `json:"Data"`
36- }
37-
38- var urlall string
3923var url1 string
4024
4125func NewschemaCmd () * cobra.Command {
@@ -59,7 +43,7 @@ func NewschemaCmd() *cobra.Command {
5943}
6044
6145func add (temp int ) error {
62-
46+ //checking if it is devoloper plan
6347 res , err := api .GetSites ()
6448 if err != nil {
6549 return err
@@ -70,48 +54,71 @@ func add(temp int) error {
7054 return nil
7155 }
7256
57+ //changing enabled to true based on field number entered
7358 conf := config .GetInstance ()
74- urlall = conf .AdminConsoleAPIDomain + "/platform-configuration/platform-registration-fields?"
75-
76- var resultResp schemaStr
77- resp , err := request .Rest (http .MethodGet , urlall , nil , "" )
78-
79- if err != nil {
80- return err
81- }
82-
83- err = json .Unmarshal (resp , & resultResp )
59+ resultResp , err := api .GetFields ("all" )
8460 if err != nil {
85- return err
61+ return nil
8662 }
8763 var temp1 []int
8864 for i := 0 ; i < len (resultResp .Data ); i ++ {
8965 if resultResp .Data [i ].Parent == "" {
9066 temp1 = append (temp1 , i )
9167 }
9268 }
93- url1 = conf .AdminConsoleAPIDomain + "/platform-configuration/registration-form-settings?"
94- var resultResp1 schemaStr
95- resp , err = request .Rest (http .MethodGet , url1 , nil , "" )
69+ resultResp1 , err := api .GetFields ("active" )
70+ if err != nil {
71+ return err
72+ }
73+ if temp > len (temp1 ) || 0 > temp {
74+ fmt .Println ("please run 'lr get schema -all' first. Please enter the field number accordingly" )
75+ return nil
76+ }
77+ resultResp .Data [temp1 [temp - 1 ]].Enabled = true
78+
79+ //Changing the display name
80+ ChangeDisplay (resultResp , temp1 , temp )
81+
82+ //Required or not
83+ IsRequired (resultResp , temp1 , temp )
84+
85+ //Advance configuration setup
86+ AdvancedConfig (resultResp , temp1 , temp )
87+
88+ //Adding the field to the configuration
89+ resultResp1 .Data = append (resultResp1 .Data , resultResp .Data [temp1 [temp - 1 ]])
90+ body , _ := json .Marshal (resultResp1 )
91+ url1 = conf .AdminConsoleAPIDomain + "/platform-configuration/default-fields?"
92+ var resultResp2 api.ResultResp
93+ resp , err := request .Rest (http .MethodPost , url1 , nil , string (body ))
9694
9795 if err != nil {
9896 return err
9997 }
10098
101- err = json .Unmarshal (resp , & resultResp1 )
99+ err = json .Unmarshal (resp , & resultResp2 )
102100 if err != nil {
103101 return err
104102 }
105- resultResp .Data [temp1 [temp - 1 ]].Enabled = true
103+ fmt .Println ("Your field has been sucessfully added" )
104+
105+ return nil
106+ }
107+
108+ func ChangeDisplay (resultResp * api.ResultResp , temp1 []int , temp int ) {
106109 var DisplayName string
107- var req string
108110 fmt .Print ("Enter the Display Name (" + resultResp .Data [temp1 [temp - 1 ]].Display + ") :" )
109111 scanner := bufio .NewScanner (os .Stdin )
110112 scanner .Scan ()
111113 DisplayName = scanner .Text ()
112114 if DisplayName == "" {
113115 DisplayName = resultResp .Data [temp1 [temp - 1 ]].Display
114116 }
117+ resultResp .Data [temp1 [temp - 1 ]].Display = DisplayName
118+ }
119+
120+ func IsRequired (resultResp * api.ResultResp , temp1 []int , temp int ) {
121+ var req string
115122 fmt .Print ("Is Required (y/n):" )
116123 fmt .Scanln (& req )
117124 for req != "Y" && req != "y" && req != "N" && req != "n" {
@@ -123,23 +130,44 @@ func add(temp int) error {
123130 } else if req == "N" || req == "n" {
124131 resultResp .Data [temp1 [temp - 1 ]].IsMandatory = false
125132 }
126- resultResp .Data [temp1 [temp - 1 ]].Display = DisplayName
127-
128- resultResp1 .Data = append (resultResp1 .Data , resultResp .Data [temp1 [temp - 1 ]])
129- body , _ := json .Marshal (resultResp1 )
130- url1 = conf .AdminConsoleAPIDomain + "/platform-configuration/default-fields?"
131- var resultResp2 schemaStr
132- resp , err = request .Rest (http .MethodPost , url1 , nil , string (body ))
133+ }
133134
134- if err != nil {
135- return err
135+ func AdvancedConfig (resultResp * api.ResultResp , temp1 []int , temp int ) {
136+ var req string
137+ var ind int
138+ var valStr string
139+ fmt .Print ("Do you want to setup advance configuration (y/n):" )
140+ fmt .Scanln (& req )
141+ for req != "Y" && req != "y" && req != "N" && req != "n" {
142+ fmt .Print ("Please enter (y/n):" )
143+ fmt .Scanln (& req )
136144 }
145+ if req == "Y" || req == "y" {
146+ fmt .Println ("select feild type" )
147+ for i := 0 ; i < len (api .TypeMap ); i ++ {
148+ fmt .Print (i + 1 )
149+ fmt .Println (": " + api .TypeMap [i + 1 ].Name )
150+ }
137151
138- err = json .Unmarshal (resp , & resultResp2 )
139- if err != nil {
140- return err
141- }
142- fmt .Println ("Your field has been sucessfully added" )
152+ fmt .Print ("Select a number from 1 to " + fmt .Sprint (len (api .TypeMap )) + ":" )
153+ fmt .Scanln (& ind )
154+ resultResp .Data [temp1 [temp - 1 ]].Type = api .TypeMap [ind ].Name
155+ if api .TypeMap [ind ].ShouldDisplayValidaitonRuleInput {
156+ fmt .Print ("Enter the validation string(for more info check https://www.loginradius.com/docs/libraries/js-libraries/javascript-hooks/#customvalidationhook15):" )
157+ fmt .Scanln (& valStr )
158+ resultResp .Data [temp1 [temp - 1 ]].Rules = valStr
159+ }
160+ if api .TypeMap [ind ].ShouldShowOption {
161+ fmt .Print ("Enter the key value json object(example " )
162+ fmt .Println (`[{"value":"one","text":" hyd"},{"value":"two","text":" vij"},{"value":"three","text":" viz"}])` )
143163
144- return nil
164+ var record []api.Array
165+
166+ err := json .NewDecoder (os .Stdin ).Decode (& record )
167+ if err != nil {
168+ log .Fatal (err )
169+ }
170+ resultResp .Data [temp1 [temp - 1 ]].Options = record
171+ }
172+ }
145173}
0 commit comments