Skip to content

Commit

Permalink
feat: updated deps and small ts improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas authored and froid1911 committed Apr 1, 2024
1 parent 70e7855 commit 23332f0
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 75 deletions.
6 changes: 3 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"document-drive": "1.0.0-alpha.17",
"document-model": "^1.0.32",
"document-model-libs": "^1.15.0",
"document-drive": "1.0.0-alpha.23",
"document-model": "^1.0.35",
"document-model-libs": "^1.17.1",
"dotenv": "^16.4.5",
"ethers": "^5.7.2",
"express": "^4.19.2",
Expand Down
35 changes: 20 additions & 15 deletions api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/src/graphql/generated/index/nexus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface NexusGenInputs {
}
DocumentDriveStateInput: { // input type
icon?: string | null; // String
id: string; // ID!
id?: string | null; // ID
name: string; // String!
slug?: string | null; // String
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/graphql/generated/index/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type DocumentDriveState {

input DocumentDriveStateInput {
icon: String
id: ID!
id: ID
name: String!
slug: String
}
Expand Down
30 changes: 19 additions & 11 deletions api/src/modules/document-drive/drive-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import { systemType } from '../system';
import {
ListenerRevision as IListenerRevision, UpdateStatus as IUpdateStatus, StrandUpdate,
} from 'document-drive';
import { OperationScope } from 'document-model/document';
import { Operation, OperationScope } from 'document-model/document';
import stringify from 'json-stringify-deterministic';
import { getChildLogger } from '../../logger';
import { Context } from '../../graphql/server/drive/context';
import { DocumentDriveAction } from 'document-model-libs/document-drive';

const logger = getChildLogger({ msgPrefix: 'Drive Resolver' });

Expand Down Expand Up @@ -178,7 +180,7 @@ export const syncType = objectType({
listenerId: idArg(),
since: 'Date',
},
resolve: async (_parent, { listenerId, since }, ctx) => {
resolve: async (_parent, { listenerId, since }, ctx: Context) => {
if (!listenerId) throw new Error('Listener ID is required');
try {
const result = await ctx.prisma.document.pullStrands(
Expand Down Expand Up @@ -228,11 +230,12 @@ export const driveSystemQueryField = queryField('system', {

export const getDrive = queryField('drive', {
type: DocumentDriveState,
resolve: async (_parent, args, ctx) => {
resolve: async (_parent, args, ctx: Context) => {
try {
const drive = await ctx.prisma.document.getDrive(ctx.driveId ?? '1');
return drive;
} catch (e) {
logger.error(e);
return null;
}
},
Expand All @@ -243,10 +246,15 @@ export const registerListener = mutationField('registerPullResponderListener', {
args: {
filter: nonNull(InputListenerFilter),
},
resolve: async (_parent, { filter }, ctx) => {
resolve: async (_parent, { filter }, ctx: Context) => {
const result = await ctx.prisma.document.registerPullResponderListener(
ctx.driveId ?? '1',
filter,
{
branch: filter.branch?.filter(b => !!b) as string[] ?? [],
documentId: filter.documentId?.filter(b => !!b) as string[] ?? [],
documentType: filter.documentType?.filter(b => !!b) as string[] ?? [],
scope: filter.scope?.filter(b => !!b) as string[] ?? [],
},
);

return result;
Expand All @@ -259,7 +267,7 @@ export const deleteListener = mutationField('deletePullResponderListener', {
args: {
filter: nonNull(InputListenerFilter),
},
resolve: async (_parent, { filter }, ctx) => {
resolve: async (_parent, { filter }, ctx: Context) => {
const result = await ctx.prisma.document.deletePullResponderListener(
ctx.driveId ?? '1',
filter,
Expand All @@ -274,7 +282,7 @@ export const pushUpdates = mutationField('pushUpdates', {
args: {
strands: list(nonNull(InputStrandUpdate)),
},
resolve: async (_parent, { strands }, ctx) => {
resolve: async (_parent, { strands }, ctx: Context) => {
logger.info('pushUpdates')
if (!strands || strands?.length === 0) return [];

Expand All @@ -290,13 +298,13 @@ export const pushUpdates = mutationField('pushUpdates', {

const result = await ctx.prisma.document.pushUpdates(
s.driveId,
operations,
operations as Operation<DocumentDriveAction>[],
s.documentId ?? undefined,
);

if (result.status !== "SUCCESS") logger.error(result.error);

const revision = result.document.operations[s.scope].slice().pop()?.index ?? -1;
const revision = result.document?.operations[s.scope as OperationScope].slice().pop()?.index ?? -1;
return {
revision,
branch: s.branch,
Expand All @@ -317,7 +325,7 @@ export const acknowledge = mutationField('acknowledge', {
listenerId: nonNull('String'),
revisions: list(ListenerRevisionInput),
},
resolve: async (_parent, { revisions, listenerId }, ctx) => {
resolve: async (_parent, { revisions, listenerId }, ctx: Context) => {
try {
if (!listenerId || !revisions) return false;
const validEntries: IListenerRevision[] = revisions
Expand All @@ -332,7 +340,7 @@ export const acknowledge = mutationField('acknowledge', {
}));

const result = await ctx.prisma.document.processAcknowledge(
ctx.driveId,
ctx.driveId ?? "1",
listenerId,
validEntries,
);
Expand Down
24 changes: 14 additions & 10 deletions api/src/modules/document-drive/drives-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
queryField,
} from 'nexus';
import { DocumentDriveState } from './drive-resolver';
import { Context } from '../../graphql/server/drive/context';
import logger from '../../logger';

export const DocumentDriveLocalState = objectType({
name: 'DocumentDriveLocalState',
Expand All @@ -26,7 +28,7 @@ export const DocumentDriveLocalStateInput = inputObjectType({
export const DocumentDriveStateInput = inputObjectType({
name: 'DocumentDriveStateInput',
definition(t) {
t.nonNull.id('id');
t.id('id');
t.nonNull.string('name');
t.string('icon');
t.string('slug');
Expand All @@ -35,11 +37,12 @@ export const DocumentDriveStateInput = inputObjectType({

export const getDrives = queryField('drives', {
type: list('String'),
resolve: async (_parent, args, ctx) => {
resolve: async (_parent, args, ctx: Context) => {
try {
const drives = await ctx.prisma.document.getDrives();
return drives;
} catch (e) {
logger.error(e);
throw new Error('Failed to get drives.');
}
},
Expand All @@ -49,7 +52,7 @@ const addDriveResponseDefinition = objectType({
name: 'AddDriveResponse',
definition(t) {
t.nonNull.field('global', {
type: DocumentDriveState,
type: DocumentDriveState
});
t.nonNull.field('local', {
type: DocumentDriveLocalState,
Expand All @@ -64,12 +67,12 @@ export const addDrive = mutationField('addDrive', {
global: nonNull(DocumentDriveStateInput),
local: nonNull(DocumentDriveLocalStateInput),
},
resolve: async (_parent, { global, local }, ctx) => {
await ctx;
return ctx.prisma.document.addDrive({
global: { ...global, nodes: [] },
local: { ...local, listeners: [], triggers: [] },
resolve: async (_parent, { global, local }, ctx: Context) => {
const drive = await ctx.prisma.document.addDrive({
global: { id: global.id, name: global.name, icon: global.icon ?? null, slug: global.slug ?? null },
local: { availableOffline: local.availableOffline, sharingType: local.sharingType ?? null, listeners: [], triggers: [] },
});
return drive.state;
},
});

Expand All @@ -78,10 +81,11 @@ export const deleteDrive = mutationField('deleteDrive', {
args: {
id: nonNull('String'),
},
resolve: async (_parent, { id }, ctx) => {
resolve: async (_parent, { id }, ctx: Context) => {
try {
await ctx.prisma.drive.deleteDrive(id);
await ctx.prisma.document.deleteDrive(id);
} catch (e) {
logger.error(e);
return false;
}

Expand Down
37 changes: 13 additions & 24 deletions api/src/modules/document/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
Listener,
ListenerFilter,
actions,
reducer,
DocumentDriveState,
DocumentDriveAction
} from 'document-model-libs/document-drive';
Expand Down Expand Up @@ -85,21 +84,21 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
return {
addDrive: async (args: DriveInput) => {
try {
await driveServer.addDrive(args);
const drive = await driveServer.addDrive(args);
await initialize();
clearDriveCache();
return drive;
} catch (e) {
logger.error(e);
throw new Error("Couldn't add drive");
}
return {
...args,
};
},
deleteDrive: async (id: string) => {
try {
await driveServer.deleteDrive(id);
clearDriveCache();
} catch (e) {
logger.error(e);
throw new Error("Couldn't delete drive");
}

Expand All @@ -115,9 +114,8 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
} else {
return drive;
}


} catch (e) {
logger.error(e);
throw new Error("Couldn't get drive");
}
},
Expand All @@ -134,6 +132,7 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {

return driveIds;
} catch (e) {
logger.error(e);
throw new Error("Couldn't get drives");
}
},
Expand Down Expand Up @@ -214,15 +213,11 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
listenerId: uuid,
system: false,
};
let drive = await driveServer.getDrive(driveId);
drive = reducer(drive, actions.addListener({ listener }));
const operation = drive.operations.local.slice().pop();
if (!operation) {
throw new Error("Operation couldnt be applied")
}
const result = await driveServer.addDriveOperations(driveId, [operation]);

const result = await driveServer.addDriveAction(driveId, actions.addListener({ listener }));
if (result.status !== "SUCCESS") {
throw new Error(`Listener couldn't be registered: ${result.error}`);
result.error && logger.error(result.error);
throw new Error(`Listener couldn't be registered: ${result.error || result.status}`);
}

return listener;
Expand All @@ -232,16 +227,10 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
driveId: string,
listenerId: string,
) => {
let drive = await driveServer.getDrive(driveId);
drive = reducer(drive, actions.removeListener({ listenerId }));
const operation = drive.operations.local.slice().pop();
if (!operation) {
throw new Error("Operation couldnt be applied")
}

const result = await driveServer.addDriveOperations(driveId, [operation]);
const result = await driveServer.addDriveAction(driveId, actions.removeListener({ listenerId }));
if (result.status !== "SUCCESS") {
throw new Error(`Listener couldn't be deleted: ${result.error}`);
result.error && logger.error(result.error);
throw new Error(`Listener couldn't be deleted: ${result.error || result.status}`);
}

delete transmitters[driveId][listenerId];
Expand Down
Loading

0 comments on commit 23332f0

Please sign in to comment.