Skip to content

Commit

Permalink
feat: set limit to posts per day
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed Jun 3, 2022
1 parent f6c745b commit b86f80b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
3 changes: 2 additions & 1 deletion backend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ FB_PARAMS_CLIENT_ID=107235746829504040378
FB_PARAMS_AUTH_URI=https://accounts.google.com/o/oauth2/auth
FB_PARAMS_TOKEN_URI=https://oauth2.googleapis.com/token
FB_PARAMS_AUTH_PROVIDER_X509_CERT_URL=https://www.googleapis.com/oauth2/v1/certs
FB_PARAMS_CLIENT_C509_CERT_URL=https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-j8wbf%40wsne-28b5b.iam.gserviceaccount.com
FB_PARAMS_CLIENT_C509_CERT_URL=https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-j8wbf%40wsne-28b5b.iam.gserviceaccount.com
MAX_NUMBER_POSTS_PER_DAY=10
83 changes: 83 additions & 0 deletions backend/src/posts/service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { CollectionReference } from '@google-cloud/firestore';
import { PostDocument } from './document';
import { Service } from './service';

describe('Service', () => {
let old_env;
let service;
beforeEach(() => {
const postsCollection: CollectionReference<PostDocument> = null;
service = new Service(postsCollection);
});
describe('create', () => {
let postsCollectionMock;
beforeEach(() => {
old_env = process.env;
process.env = { MAX_NUMBER_POSTS_PER_DAY: '5' };
postsCollectionMock = {
where: jest.fn().mockReturnThis(),
get: jest.fn().mockResolvedValue({
size: 0,
id: '1',
data: jest.fn().mockReturnValue({
message: 'test',
date: 100000,
userId: '1234',
}),
}),
doc: jest.fn().mockReturnThis(),
set: jest.fn().mockResolvedValueOnce({}),
};
service.postsCollection = postsCollectionMock;
});
afterEach(() => {
process.env = old_env;
jest.restoreAllMocks();
});
it('should return a new post', async () => {
const post = {
message: 'test',
};

const result = await service.create(post, {
user_id: '1234',
name: 'Test',
});

expect(result).toEqual({
date: 100000,
id: '1',
message: 'test',
userId: '1234',
userName: 'Test',
});
});
describe('when user has reached max number of posts per day', () => {
let postsCollectionMock;
beforeEach(() => {
// old_env = process.env;
// process.env = { MAX_NUMBER_POSTS_PER_DAY: '5' };
postsCollectionMock = {
where: jest.fn().mockReturnThis(),
get: jest.fn().mockResolvedValueOnce({ size: 5 }),
};
service.postsCollection = postsCollectionMock;
});
afterEach(() => {
// process.env = old_env;
jest.restoreAllMocks();
});
it('throws an error', async () => {
const user = {
user_id: '1',
email: 'test@test.com',
};
const message = 'new message';

await expect(service.create({ message }, user)).rejects.toThrow(
'You have reached the limit of 5 posts per day',
);
});
});
});
});
18 changes: 18 additions & 0 deletions backend/src/posts/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ export class Service {
const { user_id: userId, name: userName } = user;
const t = dayjs(new Date()).valueOf();

const numberPostsCreatedToday = await this.postsCollection
.where('date', '>=', t - 86400000)
.where('userId', '==', userId)
.get()
.then((snapshot) => snapshot.size);

const maxNumberPostsPerDay = parseInt(
process.env.MAX_NUMBER_POSTS_PER_DAY,
10,
);
if (numberPostsCreatedToday >= maxNumberPostsPerDay) {
throw new Error(
'You have reached the limit of ' +
maxNumberPostsPerDay +
' posts per day',
);
}

const docRef = this.postsCollection.doc(t.toString());
await docRef.set({
message,
Expand Down

0 comments on commit b86f80b

Please sign in to comment.