Currently in my possession:
Two Models:
User.ts
and
Company.ts
I aim to have each User linked to only one company, so that when a user registers, a new company is automatically registered on the firestore table.
The following diagram provides a clearer illustration of my current approach:
https://i.sstatic.net/bVCCz.png
Here is how I initiate the process (within the user registration method from register.component.ts):
this.companyService.addCompany(this.companies, this.user);
And this is the operation of company.service.ts:
export class CompanyService {
companiesCollection: AngularFirestoreCollection<Company>;
constructor(private afs: AngularFirestore,
private userService: UserService) {
this.companiesCollection = this.afs.collection('companies',
ref => ref.orderBy('company_name', 'asc'));
}
addCompany(company: Company, user: User) {
this.companiesCollection.add(company).then(docRef => {
user.company_id = docRef.id;
user.uid = localStorage.getItem('userUID');
this.userService.addUser(user);
});
}
}
I believe it would be more effective to implement something like:
addCompany(company: Company): Observable<Company> {
return this.companiescollection.add(company); // THIS CODE DOESNT WORK
}
Then, utilize the subscription method in register.component.ts, as follows:
this.companyService.addCompany(this.companies).subscribe(companyRef => { this.user.company_id = companyRef.id; this.userService.add(this.user); });
How should I proceed?