Created
July 26, 2018 10:23
-
-
Save Sampath-Lokuge/a8ca41dbb4a47564ea8f9317d818d89f to your computer and use it in GitHub Desktop.
budget-group.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Injector } from '@angular/core'; | |
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore'; | |
import { BudgetGroup } from '../../models/BudgetGroup'; | |
import * as moment from 'moment'; | |
import { forEach } from 'lodash'; | |
import { AuthenticationProvider } from '../authentication/authentication'; | |
@Injectable() | |
export class BudgetGroupProvider { | |
private authenticationProvider: AuthenticationProvider; | |
constructor(private fireStore: AngularFirestore, private injector: Injector /* private authenticationProvider: AuthenticationProvider */) { | |
this.init(); | |
} | |
//init | |
init() { | |
setTimeout(() => this.authenticationProvider = this.injector.get(AuthenticationProvider)); | |
} | |
//create Budget Groups | |
async createBudgetGroups(budgetGroups: BudgetGroup[], projectId: string): Promise<void> { | |
await Promise.all(budgetGroups.map(async (bg: BudgetGroup) => { | |
this.fireStore.doc<BudgetGroup>(`projects/${projectId}/budgetGroups/${bg.id}`).set({ | |
id: bg.id, | |
name: bg.name, | |
lowerCaseName: bg.name.toLowerCase(), | |
creationTime: moment().format(), | |
}); | |
})); | |
} | |
//create Budget Group | |
createBudgetGroup(name: string, projectId: string) { | |
const budgetGroupId: string = this.fireStore.createId(); | |
this.fireStore.doc<BudgetGroup>(`projects/${projectId}/budgetGroups/${budgetGroupId}`).set({ | |
id: budgetGroupId, | |
name: name, | |
lowerCaseName: name.toLowerCase(), | |
creationTime: moment().format(), | |
}) | |
} | |
//create Temp BudgetGroup | |
createTempBudgetGroup(name: string): Promise<void> { | |
const budgetGroupId: string = this.fireStore.createId(); | |
return this.fireStore.doc<BudgetGroup>(`members/${this.authenticationProvider.member.id}/budgetGroups/${budgetGroupId}`).set({ | |
id: budgetGroupId, | |
name: name, | |
lowerCaseName: name.toLowerCase(), | |
creationTime: moment().format(), | |
}) | |
} | |
//update Budget Group | |
updateBudgetGroup(budgetGroup: BudgetGroup, projectId: string): Promise<void> { | |
return this.fireStore.doc<BudgetGroup>(`projects/${projectId}/budgetGroups/${budgetGroup.id}`).update({ | |
name: budgetGroup.name, | |
}) | |
} | |
//update temp Budget Group | |
updateTempBudgetGroup(budgetGroup: BudgetGroup): Promise<void> { | |
return this.fireStore.doc<BudgetGroup>(`members/${this.authenticationProvider.member.id}/budgetGroups/${budgetGroup.id}`).update({ | |
name: budgetGroup.name, | |
}) | |
} | |
//get all Temp Budget Groups | |
getAllTempBudgetGroups(): AngularFirestoreCollection<BudgetGroup> { | |
return this.fireStore.collection(`members/${this.authenticationProvider.member.id}/budgetGroups`); | |
} | |
//get all Budget Groups | |
getAllBudgetGroups(projectId: string): AngularFirestoreCollection<BudgetGroup> { | |
return this.fireStore.collection(`projects/${projectId}/budgetGroups`); | |
} | |
//get Specific Temp Budget Group | |
getSpecificTempBudgetGroup(name: string): AngularFirestoreCollection<BudgetGroup> { | |
return this.fireStore.collection<BudgetGroup>(`members/${this.authenticationProvider.member.id}/budgetGroups`, ref => ref | |
.where('lowerCaseName', '==', name.toLowerCase()) | |
); | |
} | |
//get Specific Budget Group | |
getSpecificBudgetGroup(name: string, projectId: string): AngularFirestoreCollection<BudgetGroup> { | |
return this.fireStore.collection<BudgetGroup>(`projects/${projectId}/budgetGroups`, ref => ref | |
.where('lowerCaseName', '==', name.toLowerCase()) | |
); | |
} | |
//delete Temporally Budget Group | |
deleteTempBudgetGroup(data: BudgetGroup) { | |
this.fireStore.doc(`members/${this.authenticationProvider.member.id}/budgetGroups/${data.id}`).delete(); | |
} | |
//delete Temporally Budget Groups | |
deleteTempBudgetGroups(data: BudgetGroup[]) { | |
forEach(data, (d) => { | |
this.fireStore.doc(`members/${this.authenticationProvider.member.id}/budgetGroups/${d.id}`).delete(); | |
}); | |
} | |
//delete Budget Group | |
deleteBudgetGroup(data: BudgetGroup, projectId: string): Promise<void> { | |
return this.fireStore.doc(`projects/${projectId}/budgetGroups/${data.id}`).delete(); | |
} | |
//get default Budget Groups | |
getDefaultBudgetGroups(): AngularFirestoreCollection<BudgetGroup> { | |
return this.fireStore.collection(`defaultBudgetGroups`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment