-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBilly.js
223 lines (169 loc) · 5.01 KB
/
Billy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//const MODULE_KEYS = ["me","members","stock","planning","customers","quoting","invoicing"]
class BillyInstance {
constructor(folderId, name){
this.id = folderId
this.name = name
}
}
class BillyInstanceModule {
constructor(fileId, name){
this.id = fileId
this.name = name
}
}
const BillyApp = (function() {
var self ={}
self.key = "billy"
self.moduleKeys = ["me","members","stock","planning","customers","quoting","invoicing"]
self.moduleKeysTranslated = [];
self.modules = {
stock: StockApp
}
self.isFolderAnInstance = function(name){
const endPattern = "- billy"
return name.endsWith(endPattern)
}
self.getModuleKeysTranslated = function(){
if(self.moduleKeysTranslated.length == 0){
self.moduleKeysTranslated = self.moduleKeys.map(k => t(`app.${k}.spreadsheet.name`))
}
return self.moduleKeysTranslated
}
self.isSpreadsheetAModule = function(name){
return self.getModuleKeysTranslated().includes(name)
}
self.getModuleKeyFromSpreadsheetName = function(name){
const i = self.getModuleKeysTranslated().findIndex(t => t == name)
return i ? self.moduleKeys[i] : undefined
}
self.getInstanceNameFromFolder = function(folderName){
const endPattern = "- billy";
return folderName.substring(0, folderName.length - endPattern.length );
}
self.findInstances = function(){
const search = DriveApp.searchFolders("title contains 'billy'")
const folders = []
while (search.hasNext()) {
const folder = search.next()
const name = folder.getName()
if(self.isFolderAnInstance(name))
folders.push(new BillyInstance(folder.getId(),getInstanceNameFromFolder(name)));
}
return folders;
}
self.findInstanceModules = function(instanceId){
const files = DriveApp.getFolderById(instanceId).getFiles() // TODO what if many files
const instanceModules = []
while (files.hasNext()) {
const file = files.next()
const name = file.getName()
// if(name.endsWith(endPattern))
if(self.isSpreadsheetAModule(name))
instanceModules.push(new BillyInstanceModule(file.getId(),name))
}
Logger.log(instanceModules)
return instanceModules
}
self.buildInstancesView = function(){
const instances = self.findInstances();
const actionSection = CardService.newCardSection()
for(const i of instances){
actionSection.addWidget(
CardService.newTextButton()
.setText(i.name)
.setOnClickAction(CardService.newAction()
.setFunctionName("pipeline")
.setParameters(
{
moduleKey: self.key,
moduleFunction: "buildInstanceView",
dtoId: i.id,
dtoName: i.name
})))
}
const builder = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Instances"))
.addSection(actionSection)
return builder.build()
}
self.buildInstanceView = function(dto){
Logger.log(dto)
const modules = self.findInstanceModules(dto.id);
const actionSection = CardService.newCardSection()
for(const m of modules){
actionSection.addWidget(CardService.newTextButton()
.setText(m.name)
//.setOnClickAction(action)
.setOpenLink(CardService.newOpenLink()
.setUrl(`https://docs.google.com/spreadsheets/d/${m.id}`)
.setOpenAs(CardService.OpenAs.FULL_SIZE)
.setOnClose(CardService.OnClose.RELOAD_ADD_ON)))
}
const builder = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle(dto.name))
if(modules.length != 0)
builder.addSection(actionSection)
return builder.build()
}
self.newParty = function(){
return new Party()
}
class Party {
constructor() {
this.name = "";
this.nameTitle = "";
this.phoneNumber = "";
this.email = "";
this.addressLine1 = "";
this.addressLine2 = "";
this.addressLine3 = "";
this.bankAccountNumber = "";
this.vatNumber = "";
}
}
return self
})()
function test(){
}
const BILLY = {
billy: BillyApp,
stock: StockApp
}
function pipeline(e){
const params = e.parameters;
return BILLY[params.moduleKey][params.moduleFunction]({
key: params.dtoKey,
id: params.dtoId,
name: params.dtoName
})
}
const MODULES = [MeApp];
class Party {
constructor() {
this.name = "";
this.nameTitle = "";
this.phoneNumber = "";
this.email = "";
this.addressLine1 = "";
this.addressLine2 = "";
this.addressLine3 = "";
this.bankAccountNumber = "";
this.vatNumber = "";
}
}
function initialiseEnvironment(){
const rootFolder = getRootFolder()
for(m of MODULES){
const fileName = t(`app.${m.key}.spreadsheet.name`)
const files = rootFolder.getFilesByName(fileName)
if(files.hasNext() == false){
const ss = SpreadsheetApp.create(fileName);
m.initializeSpreadsheet(ss);
DriveApp.getFileById(ss.getId()).moveTo(rootFolder);
}
}
}
function getRootFolder(){
const rootFolder = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getParents().next()
return rootFolder
}