@ -1,9 +1,12 @@
import * as crypto from "crypto" ;
import * as crypto from "crypto" ;
import { GraphQLError } from "graphql" ;
import * as sqz from "sequelize" ;
import * as sqz from "sequelize" ;
import { Sequelize } from "sequelize-typescript" ;
import { Sequelize } from "sequelize-typescript" ;
import { ActivityNotFoundError } from "./errors/ActivityNotFoundError" ;
import { ChatNotFoundError } from "./errors/ChatNotFoundError" ;
import { ChatNotFoundError } from "./errors/ChatNotFoundError" ;
import { DuplicatedRequestError } from "./errors/DuplicatedRequestError" ;
import { DuplicatedRequestError } from "./errors/DuplicatedRequestError" ;
import { EmailAlreadyRegisteredError } from "./errors/EmailAlreadyRegisteredError" ;
import { EmailAlreadyRegisteredError } from "./errors/EmailAlreadyRegisteredError" ;
import { PostNotFoundGqlError } from "./errors/graphqlErrors" ;
import { GroupAlreadyExistsError } from "./errors/GroupAlreadyExistsError" ;
import { GroupAlreadyExistsError } from "./errors/GroupAlreadyExistsError" ;
import { GroupNotFoundError } from "./errors/GroupNotFoundError" ;
import { GroupNotFoundError } from "./errors/GroupNotFoundError" ;
import { InvalidLoginError } from "./errors/InvalidLoginError" ;
import { InvalidLoginError } from "./errors/InvalidLoginError" ;
@ -11,6 +14,7 @@ import {NoActionSpecifiedError} from "./errors/NoActionSpecifiedError";
import { UserNotFoundError } from "./errors/UserNotFoundError" ;
import { UserNotFoundError } from "./errors/UserNotFoundError" ;
import globals from "./globals" ;
import globals from "./globals" ;
import { InternalEvents } from "./InternalEvents" ;
import { InternalEvents } from "./InternalEvents" ;
import { Activity } from "./models" ;
import * as models from "./models" ;
import * as models from "./models" ;
/ * *
/ * *
@ -54,6 +58,7 @@ namespace dataaccess {
models . GroupMember ,
models . GroupMember ,
models . EventParticipant ,
models . EventParticipant ,
models . Event ,
models . Event ,
models . Activity ,
] ) ;
] ) ;
} catch ( err ) {
} catch ( err ) {
globals . logger . error ( err . message ) ;
globals . logger . error ( err . message ) ;
@ -165,21 +170,38 @@ namespace dataaccess {
* Creates a post
* Creates a post
* @param content
* @param content
* @param authorId
* @param authorId
* @param type
* @param activityId
* /
* /
export async function createPost ( content : string , authorId : number , type ? : string ) : Promise < models.Post > {
export async function createPost ( content : string , authorId : number , activityId? : number ) : Promise < models.Post > {
type = type || "MISC" ;
const activity = await models . Activity . findByPk ( activityId ) ;
const post = await models . Post . create ( { content , authorId } ) ;
if ( ! activityId || activity ) {
globals . internalEmitter . emit ( InternalEvents . POSTCREATE , post ) ;
const post = await models . Post . create ( { content , authorId , activityId } ) ;
return post ;
globals . internalEmitter . emit ( InternalEvents . POSTCREATE , post ) ;
if ( activity ) {
const user = await models . User . findByPk ( authorId ) ;
user . rankpoints += activity . points ;
await user . save ( ) ;
}
return post ;
} else {
throw new ActivityNotFoundError ( activityId ) ;
}
}
}
/ * *
/ * *
* Deletes a post
* Deletes a post
* @param postId
* @param postId
* /
* /
export async function deletePost ( postId : number ) : Promise < boolean > {
export async function deletePost ( postId : number ) : Promise < boolean | GraphQLError > {
await ( await models . Post . findByPk ( postId ) ) . destroy ( ) ;
try {
const post = await models . Post . findByPk ( postId , { include : [ { model : Activity } , { association : "rAuthor" } ] } ) ;
const activity = await post . activity ( ) ;
const author = await post . author ( ) ;
author . rankpoints -= activity . points ;
await author . save ( ) ;
} catch ( err ) {
return new PostNotFoundGqlError ( postId ) ;
}
return true ;
return true ;
}
}