Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add duration to access pass #110

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/application/service/access-sharing/CreateAccessPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class CreateAccessPass {
throw new AccessPassFailedError(AccessPassFailureReason.SHARING_CODE_EXPIRED);
}

const accessPass = new AccessPass(new UserId(userId), sharingCode.userId);
const accessPass = new AccessPass(new UserId(userId), sharingCode.userId, sharingCode.accessDuration);

return this.accessPassRepository.save(accessPass);
}
Expand Down
21 changes: 21 additions & 0 deletions src/database/migrations/V8__addDurationToAccessPass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import knex from 'knex';

const ACCESS_PASS_TABLE = 'access_pass';

export async function up(db: knex) {
await db.schema.table(ACCESS_PASS_TABLE, (table) => {
table.integer('duration');
});

await db(ACCESS_PASS_TABLE).where({ duration: null }).update({ duration: 60 });

await db.schema.alterTable(ACCESS_PASS_TABLE, (table) => {
table.integer('duration').notNullable().alter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could do this on a table-default level if you'd like with defaultTo instead, but this is also nice

Copy link
Contributor Author

@StevePole StevePole May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that work retrospectively as well? i.e. To rows that already exist?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't work retrospectively.

});
}

export async function down(db: knex) {
await db.schema.alterTable(ACCESS_PASS_TABLE, (table) => {
table.dropColumn('duration');
});
}
10 changes: 7 additions & 3 deletions src/domain/model/accessPass/AccessPass.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Access Pass', () => {
const actorUserId = new UserId();
const subjectUserId = new UserId();

const accessPass = new AccessPass(actorUserId, subjectUserId);
const accessPass = new AccessPass(actorUserId, subjectUserId, 120);

expect(accessPass.actorUserId).toBe(actorUserId);
expect(accessPass.subjectUserId).toBe(subjectUserId);
Expand All @@ -25,11 +25,15 @@ describe('Access Pass', () => {

MockDate.set('2020-11-03 00:00:00');

const accessPass = new AccessPass(actorUserId, subjectUserId);
const accessPass = new AccessPass(actorUserId, subjectUserId, 120);

// More than default duration
MockDate.set('2020-11-03 01:01:00');

expect(accessPass.isExpired()).toBe(false);

MockDate.set('2020-11-03 01:01:00');
// More than specified duration
MockDate.set('2020-11-03 02:01:00');

expect(accessPass.isExpired()).toBe(true);
});
Expand Down
7 changes: 4 additions & 3 deletions src/domain/model/accessPass/AccessPass.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { v4 as uuidv4 } from 'uuid';
import { UserId } from '../user/UserId';

// 1 hour
const ACCESS_REQUEST_LIFETIME_MSEC = 60 * 60 * 1_000;
const DEFAULT_DURATION_MINUTES = 60;

export class AccessPass {
constructor(
readonly actorUserId: UserId,
readonly subjectUserId: UserId,
readonly duration: number = DEFAULT_DURATION_MINUTES,
readonly id: string = uuidv4(),
readonly creationTime: Date = new Date()
) {}
Expand All @@ -17,6 +17,7 @@ export class AccessPass {
}

public expirationTime(): Date {
return new Date(this.creationTime.getTime() + ACCESS_REQUEST_LIFETIME_MSEC);
const millisecondDuration = this.duration * 60 * 1_000;
return new Date(this.creationTime.getTime() + millisecondDuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('PsqlAccessPassRepository', () => {
const actorUserId = new UserId();
const subjectUserId = new UserId();

const accessPass = new AccessPass(actorUserId, subjectUserId);
const accessPass = new AccessPass(actorUserId, subjectUserId, 120);
await psqlAccessPassRepository.save(accessPass);

const persistedAccessPass = await psqlAccessPassRepository.findByUserIds(actorUserId, subjectUserId);
Expand All @@ -28,11 +28,11 @@ describe('PsqlAccessPassRepository', () => {
const actorUserId = new UserId();
const subjectUserId = new UserId();

const firstPass = new AccessPass(actorUserId, subjectUserId, uuidv4(), new Date('2020-01-01'));
const firstPass = new AccessPass(actorUserId, subjectUserId, 60, uuidv4(), new Date('2020-01-01'));

const secondPass = new AccessPass(actorUserId, subjectUserId, uuidv4(), new Date('2020-01-02'));
const secondPass = new AccessPass(actorUserId, subjectUserId, 120, uuidv4(), new Date('2020-01-02'));

const thirdPass = new AccessPass(actorUserId, subjectUserId, uuidv4(), new Date('2020-01-03'));
const thirdPass = new AccessPass(actorUserId, subjectUserId, 180, uuidv4(), new Date('2020-01-03'));

await psqlAccessPassRepository.save(firstPass);
await psqlAccessPassRepository.save(thirdPass);
Expand Down
7 changes: 5 additions & 2 deletions src/infrastructure/persistence/PsqlAccessPassRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class PsqlAccessPassRepository implements AccessPassRepository {
'id',
'actor_user_id as actorUserId',
'subject_user_id as subjectUserId',
'duration',
'creation_time as creationTime',
])
.where({
Expand All @@ -30,6 +31,7 @@ export class PsqlAccessPassRepository implements AccessPassRepository {
return new AccessPass(
new UserId(linkRow.actorUserId),
new UserId(linkRow.subjectUserId),
linkRow.duration,
linkRow.id,
linkRow.creationTime
);
Expand All @@ -39,13 +41,14 @@ export class PsqlAccessPassRepository implements AccessPassRepository {
return await this.db
.raw(
`
insert into "${ACCESS_PASS_TABLE_NAME}" (id, actor_user_id, subject_user_id, creation_time)
values (:id, :actor_user_id, :subject_user_id, :creation_time)
insert into "${ACCESS_PASS_TABLE_NAME}" (id, actor_user_id, subject_user_id, duration, creation_time)
values (:id, :actor_user_id, :subject_user_id, :duration, :creation_time)
`,
{
id: accessPass.id,
actor_user_id: accessPass.actorUserId.value,
subject_user_id: accessPass.subjectUserId.value,
duration: accessPass.duration,
creation_time: accessPass.creationTime,
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getTokenForUser } from '../../../test/authentication';
import { v4 as uuidv4 } from 'uuid';
import { aUserWithAllInformation } from '../../../test/domainFactories';
import { RootController } from '../RootController';
import MockDate from 'mockdate';

describe('sharing code endpoints', () => {
const app = new RootController().expressApp();
Expand Down Expand Up @@ -68,9 +69,11 @@ describe('sharing code endpoints', () => {
await userRepository.save(user1);
await userRepository.save(user2);

const sharingCode = new SharingCode(user2.id, 60, uuidv4(), new Date());
const sharingCode = new SharingCode(user2.id, 120, uuidv4(), new Date());
await sharingCodeRepository.save(sharingCode);

MockDate.set('2020-05-04T00:00:00Z');

await request(app)
.post(`/api/v1/users/${user1.id.value}/access-passes`)
.send({ code: sharingCode.code })
Expand All @@ -79,7 +82,7 @@ describe('sharing code endpoints', () => {
.expect((response) => {
const accessPass = response.body;
expect(accessPass.userId).toBe(user2.id.value);
expect(accessPass.expiryTime).toBeDefined();
expect(accessPass.expiryTime).toBe('2020-05-04T02:00:00.000Z');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/api/tests/TestController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('TestController', () => {
const subjectUser = await userRepository.save(aNewUser());
await testRepository.save(aTest(subjectUser.id));

const accessPass = new AccessPass(actorUser.id, subjectUser.id, uuidv4(), new Date('1970-01-01'));
const accessPass = new AccessPass(actorUser.id, subjectUser.id, 60, uuidv4(), new Date('1970-01-01'));

await accessPassRepository.save(accessPass);

Expand Down
2 changes: 1 addition & 1 deletion src/presentation/api/users/UserController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('user endpoints', () => {
const actorUser = await userRepository.save(aNewUser());
const subjectUser = await userRepository.save(aNewUser());

const accessPass = new AccessPass(actorUser.id, subjectUser.id, uuidv4(), new Date('1970-01-01'));
const accessPass = new AccessPass(actorUser.id, subjectUser.id, 60, uuidv4(), new Date('1970-01-01'));

await accessPassRepository.save(accessPass);

Expand Down