-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
1973 lines (1875 loc) · 64.5 KB
/
db.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import mongoose from "mongoose";
import axios from "axios";
import { user, blog, ngo, UserBlogSchema, emergency, dev } from "./mongoose.js";
import { DB_URL } from "./config.js";
import { searchBlog, stateList } from "./helpers.js";
Object.prototype.keys = () => Object.keys(this)
Object.prototype.values = () => Object.values(this)
try {
mongoose.connect(`${DB_URL}`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
}
catch {
console.log("Not able to connect to the database")
}
class CollectionObject {
/**
* The parent class of all the classes in mentioned in the documentation
* @param {String} id The ID of document in the collection that is specified
* @param {mongoose.Model} col The mongoose model of the Collection that is to be specified
*/
constructor(id, col) {
this.id = id
this.col = col
}
/**
* Verifies if the user exists in the database
* @returns Boolean
*/
async verify() {
return (await this.col.findById(this.id))
}
__removeVersionInfo(obj) {
delete obj.__v
}
/**
*
* @returns Array<User|Blog|NGO> Objects
*/
async findAll() {
return await this.col.find()
}
/**
*
* @param {String} id ID of the User | Blog | NGO in the database
* @returns User|Blog|NGO Object from the database
*/
async findById(id) {
var obj = this.col.findById(id)
if(!obj) {
return
}
this.__removeVersionInfo(obj)
return obj
}
async extractids() {
return (await this.col.find()).map(__doc => __doc._id)
}
/**
* Checks if mentioned email is valid according to the international email format.
* @param {String} email The email ID to check the validation
* @returns Boolean
*/
isEmailValid(email) {
return /^[a-z0-9_\.-]+\@[a-z0-9\-]+\.[a-z]+/.test(email)
}
/**
* Checks if a username is already present in a given collection or not and returns a boolean.
* @param {String} username The username to check with the database collection
* @returns Boolean
*/
async usernameAlreadyExists(username) {
return Boolean((await this.findAll()).filter(__obj => __obj._id === username).length)
}
/**
* Checks if a phone number is valid or not.
* @param {String} phone The phone number that is to be validated
* @returns Boolean
*/
isPhoneValid(phone) {
return /^\+91([0-9]){10}$/.test(phone)
}
/**
* Takes the phone with the database and checks if it is already used or not
* @param {String} phone The phone number that is to be checked with the database
* @returns Boolean
*/
async phoneAlreadyExists(phone) {
return Boolean((await this.findAll()).filter(__obj => __obj.phone === phone).length)
}
/**
* Takes an email input of an email address and checks if it already used or not
* @param {String} email The email that is to be checked checked with the database
* @returns Boolean
*/
async emailAlreadyExists(email) {
return Boolean((await this.findAll()).filter(__obj => __obj.email === email).length)
}
}
/**
* The definition of the miniature of Blog object that is stored in the user collection.
* @typedef {Object} UserBlogSchema The miniature version of the blog stored in the user collection for faster access
* @property {String} _id The id of the blog that is to be stored.
* @property {String} name The name of the blog that is to be stored.
* @property {String=} description The description of the blog that is to be stored.
*/
/**
* The default error object that is used over all the code.
* @typedef {Object} Error The default error object
* @property {Number} status The HTTP status code of the error
* @property {String!} error The error that is generated
* @property {String!} comment A possible fix that can be done for the error occurred
*/
/**
* The default format for Emergency Contacts of the user.
* @typedef {Object} EmergencyContact The emergency contact of the user
* @property {Number} _id The priority of user with which the emergency contact is related to.
* @property {String} name The name of the emergency contact
* @property {String} relation The relationship of the emergency contact of the user
* @property {String} phone The phone number of the emergency contact
* @property {String} email The email of the emergency contact
*/
class UserObject extends CollectionObject {
/**
* The main class for all the user collection operations that can be done with the users or volunteers.
* @param {String} id ID of the user to be saved in the database
* @param {String=} firstName First Name of the user to be saved in the database
* @param {String=} lastName Last Name of the user to be saved in the database
* @param {String=} encryption Password of the user to be saved in the database
* @param {String=} email Email of the user to be saved in the database
* @param {Array<UserBlogSchema>=} blogs List of Simplified blogs to be saved in the database
* @param {Array<EmergencyContact>=} emergencyContacts The list of emergency contact of the user. (Max length of the list should not exceed 3)
*/
constructor(id, firstName, lastName, encryption, email, blogs, emergencyContacts) {
super(id, user)
this.col = user
this.id = id
this.firstName = firstName
this.lastName = lastName
this.encryption = encryption
this.email = email
this.blogs = blogs
this.emergencyContacts = emergencyContacts
this.doc = {
_id: this.id,
name: {
first: this.firstName,
last: this.lastName,
},
email: this.email,
encryption: this.encryption,
blogs: this.blogs,
emergencyContacts: emergencyContacts
}
}
async create() {
var __user = await this.verify()
if(__user) {
return new UserObject(
__user._id,
__user.first.name,
__user.last.name,
__user.encryption,
__user.email,
__user.blogs
)
}
if(await this.usernameAlreadyExists(this.id)) {
return {
status: 404,
error: "BadRequest! The provided username already exists.",
comment: "Use a different username"
}
}
if(await this.emailAlreadyExists(this.email)) {
return {
status: 404,
error: "BadRequest! The provided email already exists.",
comment: "Try with a different email"
}
}
if(!this.isEmailValid(this.email)) {
return {
status: 404,
error: "BadRequest! The given email is not valid!",
comment: "Use static method to change the email or dynamically change the email if not valid!"
}
}
__user = await (new user({
_id: this.id,
name: {
first: this.firstName,
last: this.lastName
},
encryption: this.encryption,
email: this.email,
emergencyContacts: this.emergencyContacts
})).save()
__user = __user._doc
this.__removeVersionInfo(__user)
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
/**
*
* @param {String} newEmail New email of the user to update in the database
* @returns UserObject
*/
async updateEmail(newEmail) {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
if(!this.isEmailValid(newEmail)) {
return {
status: 400,
comment: "The mentioned email is not valid"
}
}
__user = (await __user.set("email", newEmail).save())._doc
this.__removeVersionInfo(__user)
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
/**
*
* @param {String} newEncryption New password of the user to be updated in the database
* @returns UserObject
*/
async updateEncryption(newEncryption) {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
__user = (await __user.set("encryption", newEncryption).save())._doc
this.__removeVersionInfo(__user)
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
/**
*
* @param {String} newFirstName New first name of the user to be saved in the database
* @param {String} newLastName New last name of the user to be saved in the database
* @returns UserObject
*/
async updateName(newFirstName, newLastName) {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
var newName = {
first: this.firstName,
last: this.lastName
}
if(newFirstName && newFirstName.length > 0) {
newName.first = newFirstName
}
if(newLastName && newLastName.length > 0) {
newName.last = newLastName
}
__user = (await __user.set("name", newName).save())._doc
this.__removeVersionInfo(__user)
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
async updateUserName(newUserName) {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
if(this.id === newUserName) {
return this
}
if(await this.usernameAlreadyExists(newUserName)) {
return {
status: 404,
comment: "Bad Request! The provided new username already exists in the database."
}
}
await this.delete()
__user = await new UserObject(
newUserName,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
).create()
return __user
}
async delete() {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
var data = await this.col.findByIdAndDelete(this.id)
return data
}
extractBlogIds(user) {
return user.blogs.map(__blog => __blog._id)
}
async updateBlog(id, newName, newDescription) {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
var __blog_ids = this.extractBlogIds(__user)
if(!__blog_ids.includes(id)) {
return {
status: 404,
comment: "BadRequest! There no such blog written by this user."
}
}
var __blog = __user.blogs[__blog_ids.indexOf(id)]
if(!(!newName || newName.length === 0)) {
__blog.name = newName
}
if(!(!newDescription || newDescription.length === 0)) {
__blog.description = newDescription
}
__user = (await __user.set("blogs", __user.blogs).save())._doc
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
async newBlog(name, description) {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
var __doc = {
_id: this.id+"__blog__"+(__user.blogs.length+1),
name: name
}
if(description) {
__doc.description = description
}
__user.blogs.push(__doc)
__user = (await __user.set("blogs", __user.blogs).save())._doc
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
async deleteBlog(id) {
var __user = await this.verify()
if(!__user) {
return {
status: 400,
comment: "The requested id is not in the database"
}
}
var __blog_ids = this.extractBlogIds(__user)
if(!__blog_ids.includes(id)) {
return {
status: 404,
comment: "BadRequest! There no such blog written by this user."
}
}
__user.blogs.splice(__blog_ids.indexOf(id), 1)
__user = (await __user.set("blogs", __user.blogs).save())._doc
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
/**
* The method to find the user by first name
* @param {String} firstName The first name of the user
* @returns User Object in collection
*/
async findByFirstName(firstName) {
return (await this.col.find({})).filter(e => e.name.first.toLowerCase().trim() === firstName.toLowerCase().trim())
}
/**
* The method to find by last name of the user
* @param {String} lastName The last name of the user
* @returns User Object in collection
*/
async findByLastName(lastName) {
return (await this.col.find({})).filter(e => e.name.last.toLowerCase().trim() === lastName.toLowerCase().trim())
}
/**
* The method to find the user by full name (first name + last name)
* @param {String} name The full name of the user
* @returns User Object in the collection
*/
async findByFullName(name) {
return (await this.col.find({})).filter(e => (e.name.first.trim() + " " + e.name.last.trim()).toLowerCase() === name.toLowerCase().trim())
}
/**
* The method to find the user by their email
* @param {String} email The email of the user
* @returns User Object in the Collection
*/
async findByEmail(email) {
return (await this.col.find({email: email}))
}
/**
* The method to extract the emergency contacts of a user
* @param {String} id The id of the user
* @returns List of Emergency Contacts
*/
async extractEmergencyContacts(id) {
return (await this.col.findById(id)).emergencyContacts
}
/**
*
* @param {Number} newPriority The new priority of the contact
* @param {String=} newName The new name of the contact
* @param {String=} newRelation The new relation of the contact
* @param {String=} newPhone The new phone of the relation
* @param {String=} newEmail
*/
async updateEmergencyContact(newPriority, newName, newRelation, newPhone, newEmail) {
var __user = await this.verify()
if(!__user) {
return {
status: 404,
comment: "Requested id is not in the database."
}
}
var emergencyContacts = __user.emergencyContacts
if(emergencyContacts.length === 0) {
emergencyContacts = [{
_id: newPriority,
name: newName,
relation: newRelation,
phone: newPhone,
email: newEmail
}]
}
else {
var index = emergencyContacts.map(e => e._id).indexOf(newPriority)
emergencyContacts[index] = {
_id: newPriority,
name: newName?newName:emergencyContacts[index].name,
relation: newRelation?newRelation:emergencyContacts[index].relation,
phone: newPhone?newPhone:emergencyContacts[index].phone,
email: newEmail?newEmail:emergencyContacts[index].email
}
}
var __user = (await __user.set("emergencyContacts", emergencyContacts).save())._doc
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
/**
* The method to delete an emergency contact
* @param {Number} id The priority of the emergency contact to delete
* @returns UserObject
*/
async deleteEmergencyContact(id) {
var __user = this.verify()
if(!__user) {
return {
status: 404,
comment: "The requested id is not in the database."
}
}
var emergencyContacts = __user.emergencyContacts
if(emergencyContacts.length === 0) {
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
var index = emergencyContacts.map(e => e._id).indexOf(newPriority)
emergencyContacts.splice(index, 1)
var __user = (await __user.set("emergencyContacts", emergencyContacts).save())._doc
return new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs,
__user.emergencyContacts
)
}
}
class BlogObject extends CollectionObject {
/**
*
* @param {String} id The ID of the blog post
* @param {String} name The name or title of the blog post
* @param {String} description A short description of the blog post saying about the content
* @param {String} content The main content of the blog post
*/
constructor(id, name, description, content) {
super(id, blog)
this.id = id
this.name = name
this.description = description
this.content = content
this.doc = {
_id: id,
name: name,
description: description,
content: content
}
}
async __verifyUser() {
var __user = this.id.split("__")
if(
(__user.length !== 3) ||
!(__user.length === 3 && __user[1] === "blog" && /[0-9]+/.test(__user[2]))
) {
return {
status: 404,
comment: "The ID of the blog is not recognized."
}
}
return (await new UserObject(__user[0], "", "", "", "", []).verify())
}
/**
* Takes the input from the constructor and
* @returns BlogObject
*/
async create() {
var __blog = await this.verify()
if(__blog) {
return new BlogObject(__blog._id, __blog.name, __blog.description, __blog.content)
}
var __user = await this.__verifyUser()
if(!__user || __user.status === 404) {
return {
status: 404,
comment: "The ID of the blog does not have the corresponding user."
}
}
__user = new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs
)
if(!this.name || this.name.length === 0) {
return {
status: 404,
comment: "Cannot create a blog without its name!"
}
}
if(!this.content || this.content.length === 0) {
return {
status: 404,
comment: "Cannot create a blog without its content!"
}
}
__blog = (await new blog({
_id: this.id,
name: this.name,
description: this.description,
content: this.content
}).save())
if(!__blog) {
return {
status: 404,
comment: "An error occurred during the creation of the blog!"
}
}
__blog = __blog._doc
var __blogs = __user.blogs.length
__user = await __user.newBlog(this.name, this.description || "")
if(__user.blogs.length-__blogs === 1) {
return new BlogObject(__blog._id, __blog.name, __blog.description, __blog.content)
}
return {
status: 404,
comment: "The blog did not get created due to internal server issue! Try again after sometime!"
}
}
/**
* This function if used for updating the blog
* @param {String} newName The new name or title of the blog post if not changed then pass empty string
* @param {String} newDescription The new description of the blog post if not changed then just pass the old description
* @param {String} newContent The updated content of the blog post if not changed then pass empty string
* @returns BlogObject
*/
async update(newName, newDescription, newContent) {
var __blog = await this.verify()
if(!__blog) {
return {
status: 404,
comment: "No such blog found in the database!"
}
}
var __user = await this.__verifyUser()
if(!__user || __user.status === 404) {
return {
status: 404,
comment: "The ID of the blog does not have the corresponding user."
}
}
__user = new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs
)
var __new_blog = {
name: __blog.name,
description: __blog.description || "",
content: __blog.content
}
if(newName && newName.length > 0) {
__new_blog.name = newName
}
if(newContent && newContent.length > 0) {
__new_blog.content = newContent
}
if(newDescription.length === 0 || !newDescription) {
delete __new_blog.description
}
else if(newDescription.length > 0) {
__new_blog.description = newDescription
}
__blog = (await __blog.set(__new_blog).save())._doc
if(!__blog) {
return {
status: 404,
comment: "The database responded with an error try fixing your method!"
}
}
__user = await __user.updateBlog(__blog._id, __blog.name, __blog.description || "")
return new BlogObject(__blog._id, __blog.name, __blog.description || "", __blog.content)
}
/**
* The inputs for this function are taken from the constructor and
* @returns blog schema
*/
async delete() {
var __blog = await this.verify()
if(!__blog) {
return {
status: 404,
comment: "No such blog found in the database!"
}
}
var __user = await this.__verifyUser()
if(!__user || __user.status === 404) {
return {
status: 404,
comment: "The ID of the blog does not have the corresponding user."
}
}
__user = new UserObject(
__user._id,
__user.name.first,
__user.name.last,
__user.encryption,
__user.email,
__user.blogs
)
__blog = await this.col.findByIdAndDelete(__blog._id)
if(!__blog) {
return {
status: 404,
comment: "It seems that there is an error contacting the database! Try again after sometime."
}
}
var __blogs = __user.blogs.length
__user = await __user.deleteBlog(__blog._id)
if(!__user || __blogs-__user.blogs.length !== 1) {
__blog = await new BlogObject(__blog._id, __blog.name, __blog.description, __blog.content).create()
return {
status: 404,
comment: "It seems that there is an error contacting the database! Try again after sometime."
}
}
return __blog
}
/**
* A search function for finding the blogs
* The search returns based on names, descriptions and content matching
* @param {String} key The key to search in the names
* @returns BlogObject[]
*/
async findBySimilarity(key) {
var __blogs = await this.findAll()
if(!__blogs || __blogs.length === 0) {
return {
status: 404,
comment: "The database refused to connect or there are no blogs on the database."
}
}
var res = []
for(var __blog of __blogs) {
if(await searchBlog(
__blog.name.toLowerCase(),
key.toLowerCase().split("").filter(__ch => /^[a-z0-9]/i.test(__ch)).join("")
)) {
res.push(__blog)
}
}
return res
}
}
class NGOObject extends CollectionObject {
/**
* The main class for all the NGO routes
* @param {String} id The ID of the NGO in the database or that is to be created
* @param {String} name The name of the NGO in the database or that is to be created
* @param {String} startTime The daily start timings of the NGO in the format of HH:mm
* @param {String} closeTime The daily close timings of the NGO in the format of HH:mm
* @param {String} days The days of the week in which the NGO operates in the format of Mon-Sat (case sensitive)
* @param {Number} latitude The latitude of the NGO's location which can be accurate upto 50m
* @param {Number} longitude The longitude of the NGO's location which be accurate upto 50m
* @param {String} phone The mobile or telephone number of the NGO which is used to be contacted with (Default Format: +919999999999)
* @param {String} email The email ID of the NGO in the database or that is to be added in the database
* @param {String} line1Address The line 1 address of the NGO required
* @param {String} line2Address The line 2 address of the NGO (This param is optional [For optional leave an empty string])
* @param {String} cityOrVillage The nearest city or village that the NGO is located in
* @param {String} state The state in which the NGO is located in
* @param {Number} pinCode The pin code of the location in which the NGO is located
*/
constructor(
id,
name,
startTime,
closeTime,
days,
latitude,
longitude,
phone,
email,
line1Address,
line2Address,
cityOrVillage,
state,
pinCode
) {
super(id, ngo)
this.id = id
this.name = name
this.startTime = startTime
this.closeTime = closeTime
this.days = days
this.latitude = latitude
this.longitude = longitude
this.phone = phone
this.email = email
this.line1Address = line1Address
this.line2Address = line2Address
this.cityOrVillage = cityOrVillage
this.state = state
this.pinCode = pinCode
this.doc = {
_id: this.id,
name: this.name,
timings: {
start: this.startTime,
close: this.closeTime,
days: this.days
},
location: {
lat: this.latitude,
long: this.longitude
},
phone: this.phone,
email: this.email,
address: {
line_1: this.line1Address,
line_2: this.line2Address,
city_village: this.cityOrVillage,
state: this.state,
pin_code: this.pinCode
}
}
}
__verifyTime(time) {
var regex = /^[0-9]{2}\:[0-9]{2}$/
if(!regex.test(time)) {
return false
}
var __time = time.split(":").map(e => Number(e))
if(!__time) {
return false
}
if((__time[0] >= 0 && __time[0] <= 23)) {
return true
}
return false
}
__verifyDay(day) {
var __days = day.split("-")
if(__days.length !== 2) {
return false
}
var __verifier = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
if(__verifier.includes(__days[0]) && __verifier.includes(__days[1])) {
return true
}
return false
}
__verifyLatitude(latitude) {
return latitude >= -90 && latitude <= 90
}
__verifyLongitude(longitude) {
return longitude >= -180 && longitude <= 180
}
__verifyPinCode(pinCode) {
return Number.isInteger(pinCode) && (pinCode >= 110000 && pinCode <= 990000)
}
__verifyState(state) {
return stateList.map(e => e.toLowerCase()).includes(state.toLowerCase())
}
/**
* Returns the pin code in formation from the given data in the class
* @returns PinCodeType
*/
async getPinCodeInfo(pinCode) {
var __data = await axios.get(`https://api.postalpincode.in/pincode/${pinCode}`).then(res => res.data)
if(__data[0].Status !== "Success") {
return false
}
__data = __data[0].PostOffice[0]
return {
city_village: __data.Block,
state: __data.State,
pin_code: pinCode
}
}
async __verifyAddress(address) {
var __address = await this.getPinCodeInfo(address.pin_code)
return __address && this.__verifyState() && __address.state.toLowerCase() === address.state.toLowerCase()
}
__return(__ngo) {
return new NGOObject(
__ngo._id,
__ngo.name,
__ngo.timings.start,
__ngo.timings.close,
__ngo.timings.days,
__ngo.location.lat,
__ngo.location.long,
__ngo.phone,
__ngo.email,
__ngo.address.line_1,
__ngo.address.line_2,
__ngo.address.city_village,
__ngo.address.state,
__ngo.address.pin_code
)
}
/**
* Returns if the class params are valid or not and checks if there is an object in the collection before hand.
* @returns Object
*/
async verifyNGO() {
var __ngo = await this.verify()
if(__ngo) {
return __ngo
}
if(!this.isEmailValid(this.email)) {
return {
status: 404,
error: "BadRequest! The given email is not valid!",