Skip to content

Commit

Permalink
🎨 Switch from tabs to 2 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
leodr committed Feb 6, 2021
1 parent c8f0a97 commit 991dacc
Show file tree
Hide file tree
Showing 67 changed files with 3,545 additions and 3,555 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["next/babel"],
"plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }]]
"presets": ["next/babel"],
"plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }]]
}
36 changes: 18 additions & 18 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
module.exports = {
extends: ["react-app", "plugin:jsx-a11y/recommended"],
plugins: ["jsx-a11y"],
rules: {
"import/no-anonymous-default-export": "error",
extends: ["react-app", "plugin:jsx-a11y/recommended"],
plugins: ["jsx-a11y"],
rules: {
"import/no-anonymous-default-export": "error",

/**
* React does not have to be in scope with Next.js
*/
"react/react-in-jsx-scope": "off",
/**
* React does not have to be in scope with Next.js
*/
"react/react-in-jsx-scope": "off",

/**
* `next/link` puts the href on the `<Link>` tag instead of the `<a>` tag.
*/
"jsx-a11y/anchor-is-valid": "off",
/**
* `next/link` puts the href on the `<Link>` tag instead of the `<a>` tag.
*/
"jsx-a11y/anchor-is-valid": "off",

/**
* This rule is deprecated and does not apply to modern browsers, which
* we are targeting here.
*/
"jsx-a11y/no-onchange": "off",
},
/**
* This rule is deprecated and does not apply to modern browsers, which
* we are targeting here.
*/
"jsx-a11y/no-onchange": "off",
},
};
8 changes: 4 additions & 4 deletions .huskyrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
hooks: {
// Run lint-staged before committing, config is defined in lintstagedrc.
"pre-commit": "lint-staged",
},
hooks: {
// Run lint-staged before committing, config is defined in lintstagedrc.
"pre-commit": "lint-staged",
},
};
12 changes: 1 addition & 11 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
{
"overrides": [
{
"files": ["*.md", "*.mdx"],
"options": {
"tabWidth": 2,
"useTabs": false
}
}
],
"proseWrap": "always",
"useTabs": true
"proseWrap": "always"
}
22 changes: 11 additions & 11 deletions api/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { FormsModule } from "./forms/forms.module";
import { SubmissionModule } from "./submission/submission.module";

@Module({
imports: [
SubmissionModule,
ConfigModule.forRoot({
isGlobal: true,
// Next.js already loads the .env file for us.
ignoreEnvFile: true,
}),
FormsModule,
],
controllers: [],
providers: [],
imports: [
SubmissionModule,
ConfigModule.forRoot({
isGlobal: true,
// Next.js already loads the .env file for us.
ignoreEnvFile: true,
}),
FormsModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
36 changes: 18 additions & 18 deletions api/firebase/firebase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import admin from "firebase-admin";
@Injectable()
@Dependencies(ConfigService)
export class FirebaseService {
private _firestore: FirebaseFirestore.Firestore;
private _firestore: FirebaseFirestore.Firestore;

constructor(private configService: ConfigService) {
if (admin.apps.length === 0) {
admin.initializeApp({
credential: admin.credential.cert({
projectId: this.configService.get("NEXT_PUBLIC_FIREBASE_PROJECT_ID"),
privateKey: this.configService
.get("FIREBASE_PRIVATE_KEY")
.replace(/\\n/g, "\n"),
clientEmail: this.configService.get("FIREBASE_CLIENT_EMAIL"),
}),
});
}
constructor(private configService: ConfigService) {
if (admin.apps.length === 0) {
admin.initializeApp({
credential: admin.credential.cert({
projectId: this.configService.get("NEXT_PUBLIC_FIREBASE_PROJECT_ID"),
privateKey: this.configService
.get("FIREBASE_PRIVATE_KEY")
.replace(/\\n/g, "\n"),
clientEmail: this.configService.get("FIREBASE_CLIENT_EMAIL"),
}),
});
}

this._firestore = admin.firestore();
}
this._firestore = admin.firestore();
}

get firestore() {
return this._firestore;
}
get firestore() {
return this._firestore;
}
}
56 changes: 28 additions & 28 deletions api/forms/forms.controller.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import {
BadRequestException,
Bind,
Controller,
Delete,
Dependencies,
Param,
BadRequestException,
Bind,
Controller,
Delete,
Dependencies,
Param,
} from "@nestjs/common";
import { FirebaseService } from "api/firebase/firebase.service";
import { FormsService } from "./forms.service";

@Controller("forms")
@Dependencies(FormsService, FirebaseService)
export class FormsController {
constructor(
private readonly formsService: FormsService,
private readonly firebaseService: FirebaseService
) {}
constructor(
private readonly formsService: FormsService,
private readonly firebaseService: FirebaseService
) {}

@Delete(":id")
@Bind(Param("id"))
async deleteForm(formId: string) {
if (!formId) {
throw new BadRequestException("You have to provide a non-empty form id.");
}
@Delete(":id")
@Bind(Param("id"))
async deleteForm(formId: string) {
if (!formId) {
throw new BadRequestException("You have to provide a non-empty form id.");
}

const doc = await this.firebaseService.firestore
.collection("forms")
.doc(formId)
.get();
const doc = await this.firebaseService.firestore
.collection("forms")
.doc(formId)
.get();

if (!doc.exists) {
throw new BadRequestException(
`A form with the id \`${formId}\` does not exist.`
);
}
if (!doc.exists) {
throw new BadRequestException(
`A form with the id \`${formId}\` does not exist.`
);
}

await this.formsService.deleteFormWithSubmissions(formId);
await this.formsService.deleteFormWithSubmissions(formId);

return "Successfully deleted form.";
}
return "Successfully deleted form.";
}
}
4 changes: 2 additions & 2 deletions api/forms/forms.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FormsController } from "./forms.controller";
import { FormsService } from "./forms.service";

@Module({
controllers: [FormsController],
providers: [FormsService, FirebaseService],
controllers: [FormsController],
providers: [FormsService, FirebaseService],
})
export class FormsModule {}
94 changes: 47 additions & 47 deletions api/forms/forms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@ import { firestore } from "firebase-admin";
@Injectable()
@Dependencies(FirebaseService)
export class FormsService {
constructor(private readonly firebaseService: FirebaseService) {}

async deleteFormWithSubmissions(formId: string) {
const firestore = this.firebaseService.firestore;

const query = firestore
.collection("submissions")
.where("formId", "==", formId)
.limit(100);

await new Promise<void>((resolve, reject) => {
this.deleteQueryBatch(query, resolve).catch(reject);
});

await firestore.collection("forms").doc(formId).delete();
}

/**
* A recursive function to delete every document in a query with batching.
*/
private async deleteQueryBatch(
query: firestore.Query,
resolve: VoidFunction
) {
const snapshot = await query.get();

const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}

const firestore = this.firebaseService.firestore;

// Delete documents in a batch
const batch = firestore.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();

// Recurse on the next process tick, to avoid exploding the stack.
process.nextTick(() => {
this.deleteQueryBatch(query, resolve);
});
}
constructor(private readonly firebaseService: FirebaseService) {}

async deleteFormWithSubmissions(formId: string) {
const firestore = this.firebaseService.firestore;

const query = firestore
.collection("submissions")
.where("formId", "==", formId)
.limit(100);

await new Promise<void>((resolve, reject) => {
this.deleteQueryBatch(query, resolve).catch(reject);
});

await firestore.collection("forms").doc(formId).delete();
}

/**
* A recursive function to delete every document in a query with batching.
*/
private async deleteQueryBatch(
query: firestore.Query,
resolve: VoidFunction
) {
const snapshot = await query.get();

const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}

const firestore = this.firebaseService.firestore;

// Delete documents in a batch
const batch = firestore.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();

// Recurse on the next process tick, to avoid exploding the stack.
process.nextTick(() => {
this.deleteQueryBatch(query, resolve);
});
}
}
44 changes: 22 additions & 22 deletions api/submission/submission.controller.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import {
BadRequestException,
Bind,
Body,
Controller,
Dependencies,
Param,
Post,
BadRequestException,
Bind,
Body,
Controller,
Dependencies,
Param,
Post,
} from "@nestjs/common";
import { JsonValue } from "type-fest";
import { SubmissionService } from "./submission.service";

@Controller("forms/:formId/submissions")
@Dependencies(SubmissionService)
export class SubmissionController {
constructor(private submissionService: SubmissionService) {}
constructor(private submissionService: SubmissionService) {}

@Post()
@Bind(Param("formId"), Body())
async submit(formId: string | undefined, body: JsonValue) {
if (!formId) {
throw new BadRequestException("No form id specified specified.");
}
@Post()
@Bind(Param("formId"), Body())
async submit(formId: string | undefined, body: JsonValue) {
if (!formId) {
throw new BadRequestException("No form id specified specified.");
}

if (typeof body !== "object" || body === null || Array.isArray(body)) {
throw new BadRequestException(
"The request body has to contain a JSON object."
);
}
if (typeof body !== "object" || body === null || Array.isArray(body)) {
throw new BadRequestException(
"The request body has to contain a JSON object."
);
}

await this.submissionService.create(formId, body);
await this.submissionService.create(formId, body);

return "Successfully submitted form.";
}
return "Successfully submitted form.";
}
}
6 changes: 3 additions & 3 deletions api/submission/submission.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { SubmissionController } from "./submission.controller";
import { SubmissionService } from "./submission.service";

@Module({
imports: [],
controllers: [SubmissionController],
providers: [SubmissionService, FirebaseService],
imports: [],
controllers: [SubmissionController],
providers: [SubmissionService, FirebaseService],
})
export class SubmissionModule {}
Loading

0 comments on commit 991dacc

Please sign in to comment.