Skip to content

Commit

Permalink
refactor!: remove activeUntil from subscriptions (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten committed Nov 7, 2023
1 parent 41e2493 commit 2a24d85
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 66 deletions.
4 changes: 0 additions & 4 deletions packages/app/pages/subscriptions/[subscriptionId]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
<DatePicker v-model="subscription.anchorDate" disabled />
</UFormGroup>

<UFormGroup label="Active until" name="activeUntil">
<DatePicker v-model="subscription.activeUntil" disabled />
</UFormGroup>

<UFormGroup label="Last payment" name="lastPayment">
<DatePicker v-model="subscription.lastPayment" disabled />
</UFormGroup>
Expand Down
14 changes: 1 addition & 13 deletions packages/server/src/api/endpoints/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getProjectFromRequest } from '~/api/helpers';
import { database } from '~/database';
import { Customer, Project } from '~/entities';
import { getPaymentProvider } from '~/payment_providers';
import { getActiveUntilDate } from '~/utils';

// eslint-disable-next-line @typescript-eslint/require-await
export async function customerEndpoints(server: FastifyInstance): Promise<void> {
Expand Down Expand Up @@ -323,18 +322,7 @@ export async function customerEndpoints(server: FastifyInstance): Promise<void>

const subscriptions = await database.subscriptions.find({ project, customer }, { populate: ['changes'] });

const _subscriptions = subscriptions.map((subscription) => {
const activeUntil = subscription.lastPayment
? getActiveUntilDate(subscription.lastPayment, subscription.anchorDate)
: undefined;

return {
...subscription.toJSON(),
activeUntil,
};
});

await reply.send(_subscriptions);
await reply.send(subscriptions.map((subscription) => subscription.toJSON()));
},
});

Expand Down
26 changes: 3 additions & 23 deletions packages/server/src/api/endpoints/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FastifyInstance } from 'fastify';
import { getProjectFromRequest } from '~/api/helpers';
import { database } from '~/database';
import { Subscription } from '~/entities';
import { getActiveUntilDate, getPeriodFromAnchorDate } from '~/utils';
import { getPeriodFromAnchorDate } from '~/utils';

// eslint-disable-next-line @typescript-eslint/require-await
export async function subscriptionEndpoints(server: FastifyInstance): Promise<void> {
Expand Down Expand Up @@ -117,18 +117,7 @@ export async function subscriptionEndpoints(server: FastifyInstance): Promise<vo

const subscriptions = await database.subscriptions.find({ project }, { populate: ['customer', 'changes'] });

const _subscriptions = subscriptions.map((subscription) => {
const activeUntil = subscription.lastPayment
? getActiveUntilDate(subscription.lastPayment, subscription.anchorDate)
: undefined;

return {
...subscription.toJSON(),
activeUntil,
};
});

await reply.send(_subscriptions);
await reply.send(subscriptions.map((subscription) => subscription.toJSON()));
},
});

Expand Down Expand Up @@ -250,16 +239,7 @@ export async function subscriptionEndpoints(server: FastifyInstance): Promise<vo
return reply.code(404).send({ error: 'Subscription not found' });
}

const activeUntil = subscription.lastPayment
? getActiveUntilDate(subscription.lastPayment, subscription.anchorDate)
: undefined;

const _subscription = {
...subscription.toJSON(),
activeUntil,
};

await reply.send(_subscription);
await reply.send(subscription.toJSON());
},
});

Expand Down
1 change: 0 additions & 1 deletion packages/server/src/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ export function addSchemas(server: FastifyInstance): void {
lastPayment: { type: 'string' },
currentPeriodStart: { type: 'string' },
currentPeriodEnd: { type: 'string' },
activeUntil: { type: 'string' },
customer: { $ref: 'Customer' },
changes: {
type: 'array',
Expand Down
21 changes: 1 addition & 20 deletions packages/server/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,9 @@ import { describe, expect, it } from 'vitest';

import dayjs from '~/lib/dayjs';

import { getActiveUntilDate, getNextPeriod, getPeriodFromAnchorDate, getPreviousPeriod } from './utils';
import { getNextPeriod, getPeriodFromAnchorDate, getPreviousPeriod } from './utils';

describe('utils', () => {
const getActiveUntilDateTests = [
// oldActiveUntil, anchorDate, activeUntil
['2022-01-15', '2022-01-15', '2022-02-14'],
['2022-02-15', '2022-01-15', '2022-03-14'],
['2022-01-31', '2022-01-31', '2022-02-27'],
['2022-02-28', '2022-01-31', '2022-03-30'],
['2022-03-31', '2022-01-31', '2022-04-29'],
];
it.each(getActiveUntilDateTests)(
'getActiveUntilDate oldActiveUntil: %s & anchorDate: %s => activeUntil: %s',
(oldActiveUntil, anchorDate, expected) => {
// when
const activeUntil = getActiveUntilDate(dayjs(oldActiveUntil).toDate(), dayjs(anchorDate).toDate());

// then
expect(dayjs(activeUntil).format('DD.MM.YYYY')).toStrictEqual(dayjs(expected).endOf('day').format('DD.MM.YYYY'));
},
);

// For example, a customer with a monthly subscription set to cycle on the 2nd of the month will
// always be billed on the 2nd.

Expand Down
5 changes: 0 additions & 5 deletions packages/server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,3 @@ export function getPreviousPeriod(nextPayment: Date, anchorDate: Date): { start:
const { start } = getPeriodFromAnchorDate(nextPayment, anchorDate);
return getPeriodFromAnchorDate(dayjs(start).subtract(1, 'day').toDate(), anchorDate);
}

export function getActiveUntilDate(oldActiveUntil: Date, anchorDate: Date): Date {
const { end } = getPeriodFromAnchorDate(oldActiveUntil, anchorDate);
return end;
}

0 comments on commit 2a24d85

Please sign in to comment.