Skip to content

Commit

Permalink
Add names to applications (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
svemat01 authored Jun 4, 2022
1 parent 0cd108a commit ac48bb7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ScylloClient } from 'scyllo';

import { Globals } from '..';
import { ApplicationV2 } from '../types/Application.type';
import { ApplicationV3 } from '../types/Application.type';
import { AuthKeyV1 } from '../types/AuthKey.type';
import { DeploymentV2 } from '../types/Deployment.type';
import { DeploymentLookupV1 } from '../types/DeploymentLookup.type';
Expand All @@ -18,7 +18,7 @@ type DBType = {
// Get authorization
migrations: MigrationState;
// Applications
applications: ApplicationV2;
applications: ApplicationV3;
// Deployments
deployments: DeploymentV2;
// DeploymentLookups
Expand Down
3 changes: 3 additions & 0 deletions src/database/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { sites_deprecate } from './migrations/06_sites_deprecate';
import { owner_indexing } from './migrations/07_owner_indexing';
import { domain_ownership } from './migrations/08_domain_ownership';
import { sites_domain } from './migrations/09_sites_domain';
import { application_name } from './migrations/10_application_name';

export type MigrationState = {
instance_id: string;
Expand Down Expand Up @@ -96,4 +97,6 @@ export const Migrations: Migration<undefined>[] = [
domain_ownership,
// Transfer Domain Ownership Data
sites_domain,
// Add name for each application based on their domain
application_name,
];
33 changes: 33 additions & 0 deletions src/database/migrations/10_application_name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ApplicationV3 } from '../../types/Application.type';
import { DomainV1 } from '../../types/Domain.type';
import { Migration } from '../migrations';

export const application_name: Migration<{
applications: ApplicationV3;
domains: DomainV1;
}> = async (database) => {
const applications = await database.selectFrom('applications', [
'domain_id',
'app_id',
]);

await database.raw('alter table applications add name text;');

for (const app of applications) {
const domain = await database.selectOneFrom('domains', ['domain'], {
domain_id: app.domain_id,
});

if (!domain) continue;

const name = domain.domain.replace(/\./g, '-');

await database.update(
'applications',
{ name },
{
app_id: app.app_id,
}
);
}
};
12 changes: 8 additions & 4 deletions src/routes/api/apps/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { Static, Type } from '@sinclair/typebox';
import { FastifyPluginAsync } from 'fastify';

import { DB } from '../../../database';
import { ApplicationV2 } from '../../../types/Application.type';
import { DomainV1 } from '../../../types/Domain.type';
import { ApplicationV3 } from '../../../types/Application.type';
import { useAuth } from '../../../util/http/useAuth';
import { log } from '../../../util/logging';
import { Poof } from '../../../util/sentry/sentryHandle';
import { generateSnowflake } from '.';
import { determineIfAuth } from './ls';

export const AppCreateRoute: FastifyPluginAsync = async (router, _options) => {
const createPayload = Type.Object({});
const createPayload = Type.Object({
name: Type.String(),
});

router.post<{
Body: Static<typeof createPayload>;
Expand All @@ -33,11 +34,14 @@ export const AppCreateRoute: FastifyPluginAsync = async (router, _options) => {
return;
}

const { name } = _request.body;

// const {} = _request.body;
// const domain_id = generateSnowflake();
const createdProject: Partial<ApplicationV2> = {
const createdProject: Partial<ApplicationV3> = {
app_id: generateSnowflake(),
owner_id: authData,
name,
// domain_id,
};
// const domain: Partial<DomainV1> = {
Expand Down
2 changes: 2 additions & 0 deletions src/types/Application.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export type ApplicationV1 = {
};

export type ApplicationV2 = ApplicationV1 & { domain_id: Snowflake };

export type ApplicationV3 = ApplicationV2 & { name: string };

0 comments on commit ac48bb7

Please sign in to comment.