Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
feat(socialStats): #56 Added social Stats model and fixed autogenerat…
Browse files Browse the repository at this point in the history
…ion issue of user social connection
  • Loading branch information
Venkatesh committed May 17, 2019
1 parent 5aa2865 commit e324e35
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 13 deletions.
47 changes: 44 additions & 3 deletions web/src/app/components/login-page/login-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AuthService } from '../../services/auth.service';
import { UserService } from '../../services/user.service';
import { User } from '../../models/user.model';
import { UserSocial } from '../../models/userSocial.model';
import { SocialStatsService } from 'src/app/services/social-stats.service';

@Component({
selector: 'app-login-page',
Expand All @@ -14,6 +15,7 @@ export class LoginPageComponent implements OnInit {
constructor(
private authService: AuthService,
private userService: UserService,
private socialStatsService: SocialStatsService,
) { }

public hasError = false;
Expand Down Expand Up @@ -52,13 +54,52 @@ export class LoginPageComponent implements OnInit {
this.userService.saveUser(user)
.subscribe((userData) => {
this.saveUserSocialDetails(userData, provider);
this.saveUserSocialStats(userData, provider);
});
}

saveUserSocialDetails(userData: User, provider: string) {
saveUserSocialStats(userData, provider: string) {
const socialStats = { ...userData, ...{ provider, createdAt: new Date().toISOString() } };
this.socialStatsService.createSocialStats(socialStats)
.subscribe((response) => {
if (!response) {
console.error('error in storing stats history');
}
});
}

saveUserSocialDetails(userData, provider: string) {
const socialDetails = {};
socialDetails[provider] = userData;
this.userService.saveUserSocial(socialDetails)
.subscribe(); // TODO: Handle success and error scenarios after social doc changes.
this.userService.checkForSocialDoc(provider, userData.userId)
.subscribe((response) => {
if (!response) {
console.error('error in checking of existing social doc');
return;
}
if (response.empty) {
this.createUserSocialDetails(socialDetails);
return;
}
this.updateUserSocialDetails(response.id, socialDetails);
});
}

createUserSocialDetails(socialDetails: UserSocial) {
this.userService.addSocialDoc(socialDetails)
.subscribe((response) => {
if (!response) {
console.error('error in creating social doc');
}
});
}

updateUserSocialDetails(id: string, socialDetails: UserSocial) {
this.userService.updateSocialDoc(id, socialDetails)
.subscribe((response) => {
if (!response) {
console.error('error in updating user social doc');
}
});
}
}
1 change: 0 additions & 1 deletion web/src/app/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class AuthGuardService implements CanActivate {
) { }

canActivate(): Observable<boolean> {

return this.auth.user.pipe(
take(1),
map(authUser => !!authUser),
Expand Down
9 changes: 9 additions & 0 deletions web/src/app/models/socialStatsHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface SocialStatsHistory {
provider: string;
userId: string;
followers: number;
following: number;
repos?: number;
tweets?: number;
createdAt: string;
}
28 changes: 24 additions & 4 deletions web/src/app/models/userSocial.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { Social } from './social.model';

export interface UserSocial {
github?: Social;
twitter?: Social;
youtube?: Social;
instagram?: Social;
github?: {
updatedAt: string,
followers: number,
following: number,
userId: string,
};
twitter?: {
updatedAt: string,
followers: number,
following: number,
userId: string,
};
youtube?: {
updatedAt: string,
followers: number,
following: number,
userId: string,
};
instagram?: {
updatedAt: string,
followers: number,
following: number,
userId: string,
};
}
1 change: 0 additions & 1 deletion web/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class AuthService {
if (typeof (response) === 'undefined') {
return null;
}

switch (provider) {
case 'github':
return normalisedUser = this.normaliseGithubUser(response);
Expand Down
32 changes: 32 additions & 0 deletions web/src/app/services/social-stats.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import {
AngularFirestore,
AngularFirestoreCollection,
} from 'angularfire2/firestore';
import { SocialStatsHistory } from '../models/socialStatsHistory';
import { Observable, from } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ErrorService } from './error.service';

@Injectable({
providedIn: 'root'
})
export class SocialStatsService {

public socialStatsHistory: AngularFirestoreCollection;

constructor(
private db: AngularFirestore,
private errorService: ErrorService,
) {
this.socialStatsHistory = this.db.collection('socialStatsHistory');
}

createSocialStats(socialStat: SocialStatsHistory): Observable<SocialStatsHistory> {
return from(this.socialStatsHistory.add(socialStat))
.pipe(
map(res => res),
catchError(error => this.errorService.logError(error))
);
}
}
28 changes: 24 additions & 4 deletions web/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { map, catchError } from 'rxjs/operators';
import {
AngularFirestore,
AngularFirestoreCollection,
AngularFirestoreDocument
QuerySnapshot,
} from 'angularfire2/firestore';
import { ErrorService } from './error.service';
import { AuthService } from './auth.service';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class UserService {
}

getUserSocialDetails(providerId: string, uid: string): Observable<UserSocial> {
return from(this.userSocial.ref.where(`${providerId}.reference`, '==', uid).get())
return from(this.userSocial.ref.where(`${providerId}.userId`, '==', uid).get())
.pipe(
map(response => this.getSocialDataFromPayload(response)),
catchError(error => this.errorService.logError(error))
Expand All @@ -57,11 +57,31 @@ export class UserService {
}

addRefID(user): UserSocial {
const normalisedResponse = { ...user.additionalUserInfo.profile, reference: user.uid };
const normalisedResponse = { ...user.additionalUserInfo.profile, userId: user.uid };
return normalisedResponse;
}

saveUserSocial(social): Observable<UserSocial> {
checkForSocialDoc(provider: string, userId: string): Observable<any> {
return from(this.userSocial.ref.where(`${provider}.userId`, '==', userId).get())
.pipe(
map((response: QuerySnapshot<UserSocial>) => {
if (response.empty) {
return { empty: response.empty };
}
return { empty: response.empty, id: response.docs.pop().id };
})
);
}

updateSocialDoc(id: string, social: UserSocial): Observable<UserSocial> {
return from(this.userSocial.doc(id).set(social))
.pipe(
map(response => this.formatUserSocial(response)),
catchError(err => this.errorService.logError(err))
);
}

addSocialDoc(social: UserSocial): Observable<UserSocial> {
return from(this.userSocial.add(social))
.pipe(
map(response => this.formatUserSocial(response)),
Expand Down

0 comments on commit e324e35

Please sign in to comment.