Skip to content

Commit

Permalink
Updating cloud functions and models for new site creation
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisAlund committed Jul 15, 2019
1 parent e685a89 commit 9830cc7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 185 deletions.
178 changes: 14 additions & 164 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -1,177 +1,27 @@
{
"indexes": [
"indexes": [],
"fieldOverrides": [
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
"collectionGroup": "user-roles",
"fieldPath": "email",
"indexes": [
{
"fieldPath": "dependencies",
"arrayConfig": "CONTAINS"
"order": "ASCENDING",
"queryScope": "COLLECTION"
},
{
"fieldPath": "rendered",
"order": "ASCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "documentType",
"order": "ASCENDING"
},
{
"fieldPath": "status",
"order": "ASCENDING"
},
{
"fieldPath": "published",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "documentType",
"order": "ASCENDING"
},
{
"fieldPath": "title",
"order": "ASCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "documentType",
"order": "ASCENDING"
},
{
"fieldPath": "title",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "documentType",
"order": "ASCENDING"
},
{
"fieldPath": "updated",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "status",
"order": "ASCENDING"
},
{
"fieldPath": "updated",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "status",
"order": "ASCENDING"
},
{
"fieldPath": "updated",
"order": "ASCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "status",
"order": "ASCENDING"
},
{
"fieldPath": "published",
"order": "ASCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "status",
"order": "ASCENDING"
"order": "DESCENDING",
"queryScope": "COLLECTION"
},
{
"fieldPath": "title",
"order": "ASCENDING"
}
]
},
{
"collectionGroup": "documents",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "status",
"order": "ASCENDING"
},
{
"fieldPath": "title",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "notifications",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "isRead",
"order": "ASCENDING"
},
{
"fieldPath": "updated",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "files",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "fileType",
"order": "ASCENDING"
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION"
},
{
"fieldPath": "updated",
"order": "DESCENDING"
"order": "ASCENDING",
"queryScope": "COLLECTION_GROUP"
}
]
}
],
"fieldOverrides": []
]
}
32 changes: 32 additions & 0 deletions functions/src/models/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ITanamUserRole } from "./user.models";

export interface ITanamBase {
id: string;
created: Date | any;
updated: Date | any;
}

export abstract class TanamBase implements ITanamBase {
id: string;
created: Date | any;
updated: Date | any;

constructor(json : ITanamBase) {
this.id = json.id;
this.created = !!json.created && !!json.created.toDate
? json.created.toDate()
: json.created;
this.updated = !!json.updated && !!json.updated.toDate
? json.updated.toDate()
: json.updated;
}

toJson(): ITanamBase {
return {
id: this.id,
created: this.created || null,
updated: this.updated || null,
} as ITanamUserRole;
}

}
2 changes: 1 addition & 1 deletion functions/src/models/cloud-functions.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class AdminCreateSiteRequest implements IAdminCreateSiteRequest {
readonly force: boolean;

constructor(json: IAdminCreateSiteRequest) {
this.id = (json.id || json.name).replace(/\W/g, '');
this.id = (json.id || json.name).replace(/[^A-Za-z0-9_-]/g, '');
this.name = json.name || json.id;
this.domain = json.domain;
this.roles = {...json.roles};
Expand Down
4 changes: 2 additions & 2 deletions functions/src/models/document-type.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class TanamDocumentType implements DocumentType {
}

static withTitle(title: string): TanamDocumentType {
const id = title.replace(/\W/g, '');
const id = title.replace(/[^A-Za-z0-9_-]/g, '');
return new TanamDocumentType({
id: id,
title: title,
Expand All @@ -111,4 +111,4 @@ export class TanamDocumentType implements DocumentType {
toString() {
return `${TanamDocumentType.name}(${this.id})`;
}
}
}
35 changes: 25 additions & 10 deletions functions/src/models/user.models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AdminTheme } from './theme.models';
import { TanamBase } from "./base";

export type TanamUserRoleType = 'superAdmin' | 'admin' | 'publisher' | 'designer' | 'reviewer';

Expand Down Expand Up @@ -50,11 +51,16 @@ export class TanamUser implements ITanamUser {
constructor(json: ITanamUser) {
this.uid = json.uid;
this.email = json.email;
this.name = json.name;
this.photoUrl = json.photoUrl;
this.prefs = json.prefs;
this.roles = !!json.roles ? json.roles.slice() : [];
this.created = json.created;
this.updated = json.updated;
this.created = !!json.created && !!json.created.toDate
? json.created.toDate()
: json.created;
this.updated = !!json.updated && !!json.updated.toDate
? json.updated.toDate()
: json.updated;
}

toJson() {
Expand All @@ -65,8 +71,8 @@ export class TanamUser implements ITanamUser {
photoUrl: this.photoUrl || null,
prefs: this.prefs || null,
roles: this.roles.slice(),
created: this.created,
updated: this.updated,
created: this.created || null,
updated: this.updated || null,
} as ITanamUser;
}

Expand All @@ -75,7 +81,7 @@ export class TanamUser implements ITanamUser {
}
}

export class TanamUserRole implements ITanamUserRole {
export class TanamUserRole extends TanamBase implements ITanamUserRole {
id: string;
uid: string;
name: string;
Expand All @@ -85,23 +91,32 @@ export class TanamUserRole implements ITanamUserRole {
updated: Date | any;

constructor(json: ITanamUserRole) {
super(json);
this.uid = json.uid;
this.name = json.name;
this.email = json.email;
this.role = json.role;
this.created = json.created;
this.updated = json.updated;
}

unchangedRoleAuth(otherRole: TanamUserRole) {
if (!otherRole) {
// Has different role if comparing to a null object
// Is not different if this role is not linked to a user since changes
// doesn't affect anything before connected to a user
return !this.uid;
}

return otherRole.uid === this.uid && otherRole.role === this.role;
}

toJson(): ITanamUserRole {
console.log(`[${TanamUserRole.name}.toJson]`);
return {
id: this.id,
...super.toJson(),
uid: this.uid || null,
name: this.name || null,
email: this.email,
role: this.role,
created: this.created,
updated: this.updated,
} as ITanamUserRole;
}

Expand Down
4 changes: 3 additions & 1 deletion functions/src/triggers/site.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const registerHost = functions.database.ref('tanam/{siteId}/domains/{hash
});

// noinspection JSUnusedGlobalSymbols
export const onNewTanamSite = functions.database.ref('tanam/new/{id}').onCreate(async (snap) => {
export const onNewTanamSite = functions.database.ref('tanam/_/new/{id}').onCreate(async (snap) => {
const newSiteData = new models.AdminCreateSiteRequest(snap.val() as models.IAdminCreateSiteRequest);
const batchWrite = admin.firestore().batch();
const newSiteBaseRef = admin.firestore().collection('tanam').doc(newSiteData.id);
Expand Down Expand Up @@ -306,6 +306,7 @@ export const onNewTanamSite = functions.database.ref('tanam/new/{id}').onCreate(
email: newSiteData.roles[role],
} as ITanamUserRole);

console.log(`${userRole.toString()}: ${JSON.stringify(userRole.toJson())}`);
batchWrite.set(userRoleRef, userRole.toJson());
}

Expand All @@ -318,6 +319,7 @@ export const onNewTanamSite = functions.database.ref('tanam/new/{id}').onCreate(


return Promise.all([
snap.ref.remove(),
taskService.fetchThemeTask(newSiteData.id, 'https://github.com/oddbit/tanam-themes/default'),
batchWrite.commit(),
]);
Expand Down
15 changes: 8 additions & 7 deletions functions/src/triggers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,21 @@ export const onUserRoleChange = functions.firestore.document('tanam/{siteId}/use
return null;
}

if ((roleBefore && roleBefore.role) === (roleAfter && roleAfter.role)) {
console.log(`Roles are unchanged. Nothing more to do.`);
if (roleBefore && roleAfter && roleBefore.unchangedRoleAuth(roleAfter)) {
console.log(`Role auth is unchanged. Nothing more to do.`);
return null;
}

const firebaseUser = await admin.auth().getUser(uid);
console.log(JSON.stringify({customClaimsBefore: firebaseUser.customClaims}));
const tanamClaims = firebaseUser.customClaims['tanam'] || {};
const customClaimsBefore = firebaseUser.customClaims || {};
console.log(JSON.stringify({customClaimsBefore}));
const tanamClaims = customClaimsBefore['tanam'] || {};
const roleResults = await siteRef.collection('user-roles').where('uid', '==', uid).get();
tanamClaims[siteId] = roleResults.docs.map(doc => new AdminTanamUserRole(doc.data() as ITanamUserRole).role);

const customClaims = {tanam: tanamClaims};
console.log(JSON.stringify({customClaimsAfter: customClaims}));
promises.push(admin.auth().setCustomUserClaims(uid, customClaims));
const customClaimsAfter = {tanam: tanamClaims};
console.log(JSON.stringify({customClaimsAfter}));
promises.push(admin.auth().setCustomUserClaims(uid, customClaimsAfter));

const userRef = siteRef.collection('users').doc(uid);
promises.push(admin.firestore().runTransaction(async (trx) => {
Expand Down

0 comments on commit 9830cc7

Please sign in to comment.