Skip to content

Commit 0f197da

Browse files
committed
add combine user script
1 parent d3ed29a commit 0f197da

File tree

4 files changed

+141
-8
lines changed

4 files changed

+141
-8
lines changed

amplify/backend/api/aray/schema/Project.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
type Project
22
@model
3+
@key(name: "byOwnerByStatus", fields: ["owner","status"], queryField: "getProjectsByOwnerByStatus")
34
@auth(rules: [
45
{ allow: public, operations: [read] },
56
# Defaults to use the "owner" field.

data/scripts/combine-users.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
const prompt = require('../prompt');
2+
const docClient = require('../docClient');
3+
4+
const dataSets = [
5+
{
6+
targetUsername: 'john6',
7+
sourceUsernames: [
8+
'John_7fe98093-a62d-4dc7-89c1-9a2813acda5d',
9+
'jhuang_7ef0dcac-d0a8-4803-846a-7f9456930cbb',
10+
],
11+
},
12+
];
13+
14+
const updateTableRecords = async (tableName, indexName, key = 'username', targetUsername, sourceUsername) => {
15+
const items = await docClient.queryAll({
16+
TableName: tableName,
17+
IndexName: indexName,
18+
KeyConditionExpression: `${key} = :username`,
19+
ExpressionAttributeValues: {
20+
':username': sourceUsername,
21+
},
22+
ScanIndexForward: false,
23+
});
24+
25+
console.log(`Update ${items.length} items for ${tableName}`);
26+
27+
if (items.length === 0) return;
28+
29+
const updatedItems = items.map((item) => {
30+
item[key] = targetUsername;
31+
32+
return item;
33+
});
34+
35+
return docClient.batchUpdate(tableName, updatedItems);
36+
};
37+
38+
(async () => {
39+
try {
40+
const {
41+
tableNames,
42+
} = await prompt();
43+
44+
const getTableName = (typeName) => {
45+
return tableNames.find((x) => x.startsWith(`${typeName}-`));
46+
};
47+
48+
await Promise.all(dataSets.map(async ({ targetUsername, sourceUsernames }) => {
49+
await Promise.all(sourceUsernames.map(async (sourceUsername) => {
50+
// pull the userProjects first
51+
const userProjects = await docClient.queryAll({
52+
TableName: getTableName('UserProject'),
53+
IndexName: 'byUser',
54+
KeyConditionExpression: `username = :username`,
55+
ExpressionAttributeValues: {
56+
':username': sourceUsername,
57+
},
58+
ScanIndexForward: false,
59+
});
60+
61+
const toUpdateProjects = [];
62+
const toUpdateUserProjects = [];
63+
// project managers
64+
if (userProjects.length > 0) {
65+
await Promise.all(userProjects.map(async (userProject) => {
66+
const { projectId } = userProject;
67+
68+
const { Item: project } = await docClient.get({
69+
TableName: getTableName('Project'),
70+
Key: { id: projectId },
71+
}).promise();
72+
73+
if (project.managers.includes(sourceUsername)) {
74+
project.managers = JSON.parse(JSON.stringify(project.managers).replace(sourceUsername, targetUsername));
75+
toUpdateProjects.push(project);
76+
}
77+
78+
if (project.owner === sourceUsername) {
79+
project.owner = targetUsername;
80+
toUpdateProjects.push(project);
81+
}
82+
83+
userProject.username = targetUsername;
84+
toUpdateUserProjects.push(userProject);
85+
}));
86+
}
87+
88+
await Promise.all([
89+
// project owner and manager
90+
toUpdateProjects.length > 0 && docClient.batchUpdate(getTableName('Project'), toUpdateProjects),
91+
// user project
92+
toUpdateUserProjects.length > 0 && docClient.batchUpdate(getTableName('UserProject'), toUpdateUserProjects),
93+
// contribution
94+
updateTableRecords(
95+
getTableName('Contribution'),
96+
'byUsernameByCreatedAt',
97+
'username',
98+
targetUsername,
99+
sourceUsername,
100+
),
101+
// statement
102+
updateTableRecords(
103+
getTableName('Statement'),
104+
'byUsernameByDate',
105+
'username',
106+
targetUsername,
107+
sourceUsername,
108+
),
109+
// user need
110+
updateTableRecords(
111+
getTableName('UserNeed'),
112+
'byUser',
113+
'username',
114+
targetUsername,
115+
sourceUsername,
116+
),
117+
// user tag
118+
updateTableRecords(
119+
getTableName('UserTag'),
120+
'byUser',
121+
'username',
122+
targetUsername,
123+
sourceUsername,
124+
),
125+
]);
126+
127+
// delete user
128+
await docClient.delete({
129+
TableName: getTableName('User'),
130+
Key: {
131+
username: sourceUsername,
132+
},
133+
}).promise();
134+
}));
135+
}));
136+
} catch (e) {
137+
console.log(e);
138+
}
139+
})();

src/components/Avatar.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ export default function CustomAvatar({
7878
setFallbackSrc(`https://robohash.org/${username}.png?set=set4`);
7979
}, [username]);
8080

81-
console.log('cache', cache);
82-
8381
if (!fallbackSrc) return null;
8482

8583
return (

src/views/User/User/User.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ export default function User({ id: inId, computedMatch, match }) {
173173
}),
174174
]);
175175

176-
console.log('data', data);
177-
console.log('userContributions', userContributions);
178-
console.log('userStatements', userStatements);
179-
180176
if (!data) return;
181177

182178
setUser(data);
@@ -226,6 +222,7 @@ export default function User({ id: inId, computedMatch, match }) {
226222
setUserProjects(data.projects.items);
227223
setNeeds(data.needs.items.map(({ need }) => need));
228224

225+
// TODO: handle combined user statements (multiple records for the same dates)
229226
let totalHoursInThePastYear = 0;
230227
const heatmapData = userStatements.map(({ date, completedHours }) => {
231228
if (date === today.format('YYYY-MM-DD')) {
@@ -251,8 +248,6 @@ export default function User({ id: inId, computedMatch, match }) {
251248
})();
252249
}, [id, lastUpdatedAt]);
253250

254-
console.log({ user, id });
255-
256251
if (!user) {
257252
return <Loading fullScreen={false} />;
258253
}

0 commit comments

Comments
 (0)