Skip to content

Commit

Permalink
chore: the first stage of analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo committed Sep 5, 2024
1 parent f779593 commit f5fa502
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 0 deletions.
23 changes: 23 additions & 0 deletions server/migrations/20240905052256_redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
await knex.schema.createTable('redirects', (table) => {
table
.uuid('id')
.unique()
.defaultTo(knex.raw('uuid_generate_v4()'))
.primary();
table.uuid('link_id').references('id').inTable('links');
table.string('ip').notNullable();
table.string('user_agent');
table.string('sec_ch_ua_platform');
table.string('accept_language');

table.timestamps(true, true);
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTable('redirects');
}
11 changes: 11 additions & 0 deletions server/src/controllers/Link/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import Services from '../../services';
import type { Reply, Request } from '../../types';

export default async function redirect(request: Request, reply: Reply) {
const headers = request.headers;
const sec_ch_ua_platform = Array.isArray(headers['sec-ch-ua-platform'])
? headers['sec-ch-ua-platform'][0]
: headers['sec-ch-ua-platform'];
const shorter_name = (request.params as { shorter_name: string })
.shorter_name;

const link = await Services.link.getByShorterName(shorter_name);
const redirect = await Services.redirect.create({
link_id: link.id,
ip: request.ip,
accept_language: headers['accept-language'],
user_agent: headers['user-agent'],
sec_ch_ua_platform: sec_ch_ua_platform,
});

return reply.redirect(link.url);
}
8 changes: 8 additions & 0 deletions server/src/services/Redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { IRedirect, IRedirectForCreate } from '@linx/shared';

export default class Redirect {
static async create(redirect: IRedirectForCreate) {
//todo: complete with the repository
return redirect;
}
}
2 changes: 2 additions & 0 deletions server/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Auth from './Auth';
import Link from './Link';
import Redirect from './Redirect';
import User from './User';

export default class Services {
static auth = Auth;
static user = User;
static link = Link;
static redirect = Redirect;
}
1 change: 1 addition & 0 deletions shared/src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './user';
export * from './server';
export * from './link';
export * from './redirect';
9 changes: 9 additions & 0 deletions shared/src/interfaces/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {
RedirectForCreateSchema,
RedirectSchema,
} from '../schema/redirect';
import type { z } from 'zod';

export interface IRedirect extends z.infer<typeof RedirectSchema> {}
export interface IRedirectForCreate
extends z.infer<typeof RedirectForCreateSchema> {}
1 change: 1 addition & 0 deletions shared/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './user';
export * from './link';
export * from './redirect';
20 changes: 20 additions & 0 deletions shared/src/schema/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { z } from 'zod';

export const RedirectSchema = z.object({
id: z.string(),
link_id: z.string(),
ip: z.string().ip(),
user_agent: z.string().optional(),
accept_language: z.string().optional(),
sec_ch_ua_platform: z.string().optional(),
created_at: z.coerce.date(),
updated_at: z.coerce.date(),
});

export const RedirectForCreateSchema = z.object({
link_id: z.string(),
ip: z.string().ip(),
sec_ch_ua_platform: z.string().optional(),
user_agent: z.string().optional(),
accept_language: z.string().optional(),
});

0 comments on commit f5fa502

Please sign in to comment.