aboutsummaryrefslogtreecommitdiff
path: root/src/db/model/Project.ts
blob: fa49fea58c31fc7a36e076b2ae12026da70a37e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { default as ContributorGroup } from './ContributorGroup';
import { default as File } from './File';
import { BelongsTo, BelongsToMany, Column, CreatedAt, DataType, ForeignKey,
		Model, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript';
import { default as User } from './User';
import { default as UserJoinsProject } from './UserJoinsProject';

@Table
export default class Project extends Model<Project> {
	@PrimaryKey
	@Column
	public name!: string;

	@BelongsToMany(() => User, () => UserJoinsProject)
	public contributors?: User[];

	@ForeignKey(() => File)
	@Column
	public rootFolderId!: string;

	@BelongsTo(() => File)
	private readonly rootFolderInternal?: File;

	@CreatedAt
	@Column(DataType.DATE)
	public creationDate!: Date;

	@UpdatedAt
	@Column(DataType.DATE)
	public lastActivity!: Date;

	public get rootFolder(): File {
		if (this.rootFolderInternal === undefined) {
			throw new Error('rootFolderInternal is undefined');
		}
		return this.rootFolderInternal;
	}

	public get properties(): object {
		return this;
	}

	public get metadata(): {
		public_metadata?: Metadata;
		private_metadata?: Metadata;
		admin_metadata?: Metadata;
	} {
		return {
			public_metadata: this.publicMetadata,
			private_metadata: this.privateMetadata,
			admin_metadata: this.adminMetadata
		};
	}

	public set metadata(newMetadata: {
		public_metadata?: Metadata;
		private_metadata?: Metadata;
		admin_metadata?: Metadata;
	}) {
		if (newMetadata.public_metadata) {
			this.publicMetadata = newMetadata.public_metadata;
		}
		if (newMetadata.private_metadata) {
			this.privateMetadata = newMetadata.private_metadata;
		}
		if (newMetadata.admin_metadata) {
			this.adminMetadata = newMetadata.admin_metadata;
		}
	}

	public get publicMetadata(): Metadata {
		return {};
	}

	public set publicMetadata(newMetadata: Metadata) {
		return;
	}

	public get privateMetadata(): Metadata {
		return {};
	}

	public set privateMetadata(newMetadata: Metadata) {
		return;
	}

	public get adminMetadata(): Metadata {
		return {};
	}

	public set adminMetadata(newMetadata: Metadata) {
		return;
	}

	public async getAccessLevel(user: User): Promise<String[]> {
		const ujps: UserJoinsProject[] | null = await UserJoinsProject.findAll({
			include: [ ContributorGroup],
			where: {
				username: user.username,
				projectName: this.name
			}
		})
		const people: String[] = await ujps.map((ujp: UserJoinsProject): String => { 
			if(ujp.contributorGroup != undefined){
				return ujp.contributorGroupName
			}
			else{
				throw new Error(`unimplemented, ${this}`);
			}
		})
		return people;
	}

	public async getUserInfo(user: User): Promise<UserInfo> {
		return {
			username: user.username,
			access_level: (await this.getAccessLevel(user)).join()
		};
	}

	public async getFullInfo(): Promise<ProjectFullInfo> {
		if (this.contributors === undefined) {
			throw new Error('contributors undefined');
		}
		//console.log(await this.getUserInfo(this.contributors[0]))
		return {
			project_name: this.name,
			users: await Promise.all(this.contributors
			.map( (u: User): Promise<UserInfo> => this.getUserInfo(u))),
			public_metadata: {
				creation_date: this.creationDate
			},
			private_metadata: {},
			admin_metadata: {}
		}
	}
}

interface Metadata {}

interface UserInfo {
	username: string;
	access_level: string;
}

export interface ProjectFullInfo {
	project_name: string;
	users: UserInfo[];
	public_metadata: Metadata;
	private_metadata?: Metadata;
	admin_metadata?: Metadata;
}